2016-07-23 30 views
0

我有TextBoxLeave事件。
TextBox.Leave子,我想顯示一個確認MSGBOX詢問用戶保存更改,但是,如果我表現出MsgBox,則不觸發Button.Click事件(TextBox.Leave事件後:看到我剛纔的問題Here趕上控制(按鈕點擊)誰提出了離開事件的文本框

所以我想插入(在TextBox.Leave子)一個代碼來捕獲按鈕的名稱(或另一個rif)被點擊並觸發了TextBox.Leave事件。

有按鈕的名稱,我就可以提高點擊事件:

DirectCast(Me.Controls("ButtonName"), Button).PerformClick() 

回答

1

而不是調用事件,你可以使用的方法來組織代碼更好。以這種方式調用代碼的問題之一是,遲早你會想知道它是如何被用戶或代碼調用的。

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click 
    ' arguably, if the user clicks a save button, 
    ' you dont ask them 
    SaveMyStuff() 
End Sub 

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave 
    If QueryUserSave("Customer") Then 
     SaveMyStuff() 
    End If 
End Sub 
Private Function QueryUserSave(whereMsg As String) As Boolean 
    Dim dlgR As DialogResult 
    Dim msg = String.Format("Some stuff has changed in {0}. Save it now?", whereMsg) 

    dlgR = MessageBox.Show(msg, "Sorry to bother you...", MessageBoxButtons.YesNo) 

    Return dlgR = Windows.Forms.DialogResult.Yes 
End Function 

Private Sub SaveMyStuff() 
    '... 
End Sub 

如果你寫一個做一個單一的東西的方法(包括事件),就可以一起從其他方法將它們鏈接到一遍又一遍地重複使用。

+0

您的解決方案總是更好:) – genespos

0
Dim CtrlName As String = Me.ActiveControl.Name 
相關問題