[英]Button.Click event doesn't fire after TextBox.Leave event
I use the below code to verify if the text into a TextBox was changed and ask for saving changes: 我使用下面的代码来验证文本到文本框中是否已更改,并要求保存更改:
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
But when I click a button (for example button1
) while the cursor is inside TextBox1
, only TextBox1.Leave
event was raised. 但是,当我在光标位于
TextBox1
内时单击按钮(例如button1
)时,只会引发TextBox1.Leave
事件。
How can I have Button?.click
event raise after TextBox1.Leave
event? 在
TextBox1.Leave
事件之后Button?.click
事件?
How can I have button1.click event raise after TextBox1.Leave event?
在TextBox1.Leave事件之后如何引发button1.click事件?
If TextBox1 has the focus and you click a button the leave event will fire before the click event. 如果TextBox1具有焦点,并且您单击按钮,则离开事件将在单击事件之前触发。 To test click in Texbox1, then click Button1.
要进行测试,请在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
Try to clarify your question please. 请尝试澄清您的问题。
edit: This recreates the problem I think you are having, 编辑:这重现了我认为您遇到的问题,
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
Did some searching and found that the Message box causes pending focus events to be lost. 进行了一些搜索,发现“消息”框导致挂起的焦点事件丢失。
Thanks to dbasnett's hints I was able to get the name of the control that fired the Leave event, so to call it (if it's a button) after the Message Box was showed: 多亏了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
Anyway Plutonix showed me a better solution here 无论如何,Plutonix 在这里 向我展示了一个更好的解决方案
You can call multiple events one after the another. 您可以一个接一个地调用多个事件。 Your TextBox1_Leave event could trigger Button1_click event.
您的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
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.