2013-04-07 75 views
0

當某個表單關閉時,我捕獲formClosing事件以檢查用戶是否真的想要關閉它。找出'formCloseing'事件被觸發時最後按下的按鈕

但是,如果單擊「下一步>」,我不希望執行此檢查,除非我知道檢查了哪個按鈕,總是會尋找構造。

有沒有一種方法可以將用戶關注的按鈕傳遞給confirm_exit()函數,這樣如果是'Next>',我可以忽略該檢查並只是重新運行'False'?

'*' 
' Carries out actions when the user clicks the 'X' button to close the form 
'*' 
Private Sub btnX(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 

    Dim clicked As Button ' The button that the user clicked 

    clicked = ??? ' How do I get the button that was last clicked? 

    e.Cancel = confirm_exit(clicked) 

End Sub 

'** 
' Checks that the user does actually want to cancel the Music Renamer 
'* 
Public Function confirm_exit(Optional clicked As Button = Nothing) As Boolean 

    ' Ask the user if they are sure that they wish to cancel the Music Renamer 
    Dim cancel As String = MsgBox("The Music Renamer is not finished yet. Are you sure you wish to quit?", vbYesNo, "Are you sure?") 

    ' Set confirm_exit to false by default 
    confirm_exit = False 

    ' Check if the user clicked 'Next >', and exit the function if they did 
    If clicked.Name = "btnNext" Then Exit Function 

    ' If the user is sure, close the Music Renamer form 
    If Not cancel = vbYes Then confirm_exit = True 

End Function 

回答

1

有很多方法可以解決這個,但最簡單的辦法是將你的窗體類添加verifyClosing布爾場的true的初始值。

無論何時您希望表單在未經確認的情況下關閉,只需在調用Close方法之前設置verifyClosing = false即可。您的結束事件處理程序可以檢查此值以決定要執行的操作。

+0

我可以看到這個想法的有效性,但它不起作用。當用戶點擊'Next>'時,我在'Me.Close()'之前設置了'verifyClosing = false',但是當我檢查'confirm_exit()'函數中的值時,它總是被設置爲'True' 。謝謝。 – 2013-04-07 12:30:35

+0

沒關係,這是因爲我錯誤地關閉了表單兩次,即使表單已經關閉,'formClosing'事件也會被觸發。謝謝您的幫助。 – 2013-04-07 15:09:28