找出触发'formCloseing'事件时最后按下的按钮

时间:2013-04-07 12:01:50

标签: vb.net button formclosing

当某个表单关闭时,我捕获formClosing事件以检查用户是否真的想要关闭它。

但是,如果'下一步>'我不希望执行此检查点击,除非我知道检查了什么按钮,总是寻求确认。

有没有办法可以将用户clicekd的按钮传递给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 个答案:

答案 0 :(得分:1)

有很多方法可以解决这个问题,但最简单的方法是在表单类中添加verifyClosing布尔字段,初始值为true

每当您希望表单在没有确认的情况下关闭时,只需在调用verifyClosing = false方法之前设置Close即可。您的结束事件处理程序可以检查此值以决定要执行的操作。