2016-07-22 81 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