0

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

但是,如果单击“下一步>”,我不希望执行此检查,除非我知道检查了哪个按钮,否则总是寻求构象。

有没有办法可以将用户单击的按钮传递给confirm_exit()函数,这样如果它是“下一步>”我可以忽略检查并重新运行“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
4

1 回答 1

1

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

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

于 2013-04-07T12:07:36.163 回答