2016-07-22 76 views
0

我用下面的代码来验证,如果文本到文本框被改变,并要求保存更改:Button.Click事件TextBox.Leave事件后不火

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事件。

我怎么能有TextBox1.Leave事件之后Button?.click事件提高?

+0

编辑这个问题,并显示Button1的Click事件以及Btn_SaveChanges单击事件。 – dbasnett

回答

1

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

如果TextBox1具有焦点并且您单击了一个按钮,那么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 

做了一些搜索,发现该消息框导致未决丢失焦点事件。

+0

button1.click事件未被触发 – genespos

+0

编辑您的问题并显示button1单击事件以及Btn_SaveChanges单击事件。 – dbasnett

+0

Button1.click就是例子。在任何地方,我点击没有事件是在'TextBox1.Leave'之后引发的 – genespos

-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 

希望这会有所帮助。

+0

但我不知道我需要提出什么事件。我点击的地方并不重要,唯一引发的事件是'TextBox1.Leave',而不是我点击的其他事件。 – genespos

+0

这不是**发射事件**。这只是调用另一种方法。这也没有解决OP的问题。 – Enigmativity

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