如果呼叫表格,事件发射了两次



任何人都可以帮助避免这种情况吗?

在事件处理程序中,我需要Cal表格,但是在卸载表格后,该事件再次发射。

    Private Sub MyHandler(sender As System.Object, e As System.EventArgs) Handles txObjName.Leave
        Dim MyVar As Integer = SomeValue
        dim myForm as SomeForm
        MyForm.ShowDialog()

关闭myform后,该事件再次触发

快速思考是尝试使用FormClosing事件。

因此您的代码看起来像这样:

Private _Closing as boolean = False
Private Sub MyHandler(sender As System.Object, e As System.EventArgs) Handles txObjName.Leave
    If Not _Closing Then
        Dim MyVar As Integer = SomeValue
        Dim myForm as SomeForm
        MyForm.ShowDialog()
    End If
End Sub
Private Sub FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    _Closing = True
End Sub

您使用FormClosing事件将布尔 _Closing切换到true,因此当闭合表单时,代码不会执行:)

解决的变化:

Handles txObjName.Leave

Handles txObjName.LostFocus

谢谢大家

最新更新