簡體   English   中英

TextBox.Leave事件后,Button.Click事件不會觸發

[英]Button.Click event doesn't fire after TextBox.Leave event

我使用下面的代碼來驗證文本到文本框中是否已更改,並要求保存更改:

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

但是,當我在光標位於TextBox1內時單擊按鈕(例如button1 )時,只會引發TextBox1.Leave事件。

TextBox1.Leave事件之后Button?.click事件?

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

如果TextBox1具有焦點,並且您單擊按鈕,則離開事件將在單擊事件之前觸發。 要進行測試,請在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

進行了一些搜索,發現“消息”框導致掛起的焦點事件丟失。

多虧了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 在這里 向我展示了一個更好的解決方案

您可以一個接一個地調用多個事件。 您的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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM