在TextBox.Leave事件之后,Button.Click事件不会触发

时间:2016-07-22 10:57:39

标签: vb.net event-handling

我使用以下代码验证TextBox中的文本是否已更改并要求保存更改:

Private TBoxTxtBefore As String = String.Empty

Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
    Try
        TBoxTxtBefore = CType(sender, TextBox).Text
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    Try
        If CType(sender, TextBox).Text <> TBoxTxtBefore Then
            Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
            If SN = vbYes Then Btn_SaveChanges.PerformClick()
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

但是,当光标在button1内时,单击按钮(例如TextBox1)时,只会引发TextBox1.Leave事件。

如何在Button?.click事件后举起TextBox1.Leave事件?

3 个答案:

答案 0 :(得分:1)

  

如何在TextBox1.Leave事件后引发button1.click事件?

如果TextBox1具有焦点并且您单击按钮,则会在click事件之前触发leave事件。要测试Texbox1中的单击,然后单击Button1。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Debug.WriteLine("B1C")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Debug.WriteLine("B2C")
End Sub

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    Debug.WriteLine("TBL")
    Button2.PerformClick()
End Sub

请尝试澄清您的问题。

编辑:这会重新创建我认为您遇到的问题,

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Debug.WriteLine("B1C")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Debug.WriteLine("B2C")
End Sub

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    MessageBox.Show("FOO")
    Debug.WriteLine("TBL")
    Button2.PerformClick()
End Sub

进行了一些搜索并发现“消息”框导致待处理的焦点事件丢失。

答案 1 :(得分:0)

感谢dbasnett的提示,我能够获得触发Leave事件的控件的名称,因此在显示消息框后调用它(如果它是一个按钮):

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    Try
        Dim FiringCtrl As Control = Me.Activecontrol
        If CType(sender, TextBox).Text <> TBoxTxtBefore _
           AndAlso FiringCtrl.Name <> "Btn_SaveChanges" Then
            Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
            If SN = vbYes Then Btn_SaveChanges.PerformClick()
            If TypeOf FiringCtrl Is Button Then 
                DirectCast(FiringCtrl, Button).PerformClick()
            End If
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

无论如何,Plutonix向我展示了更好的解决方案 here

答案 2 :(得分:-1)

您可以一个接一个地调用多个事件。您的TextBox1_Leave事件可能会触发Button1_click事件。

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

  'Take the control to another sub. Pass the sender control and the event details as arguments
  Call Button1_Click(sender,e)

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Do your stuff here

End Sub

希望这有帮助。