TextBox.Leave事件后Button.Click事件不会触发 [英] Button.Click event doesn't fire after TextBox.Leave event

查看:134
本文介绍了TextBox.Leave事件后Button.Click事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 私人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
尝试
如果CType(发件人,TextBox).Text<> TBoxTxtBefore Then
Dim SN As MsgBoxResult = MsgBox(Save changes?,vbYesNo,Save Changes)
如果SN = vbYes然后Btn_SaveChanges.PerformClick()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End尝试
End Sub

但当我点击一个按钮(例如 button1 ),而游标位于 TextBox1 内时,只有 TextBox1.Leave 事件被提出。



我如何拥有 Button? / code>事件提升后 TextBox1.Leave 事件?

解决方案

blockquote>

如何在TextBox1.Leave事件之后加载button1.click事件?


如果TextBox1有焦点,您单击一个按钮,休假事件将在点击事件之前触发。要测试点击Texbox1,然后单击Button1。

 私有子按钮1_Click(发件人作为对象,e作为EventArgs)处理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)处理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 Obj ect,e As EventArgs)Handles TextBox1.Leave
MessageBox.Show(FOO)
Debug.WriteLine(TBL)
Button2.PerformClick()
End Sub

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


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.

How can I have Button?.click event raise after TextBox1.Leave event?

解决方案

How can I have button1.click event raise after TextBox1.Leave event?

If TextBox1 has the focus and you click a button the leave event will fire before the click event. To test click in Texbox1, then click 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.

这篇关于TextBox.Leave事件后Button.Click事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆