Frage

When a certain form closes, I capture the formClosing event to check if the user actually want's to close it.

However, I don't want this check carried out if 'Next >' is clicked, unless I know what button was checked, conformation is always sought.

Is there a way I can pass the button that the user clicekd to the confirm_exit() function, so that if it was 'Next >' I can ignore the check and just retrun '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
War es hilfreich?

Lösung

There are many ways to approach this, but the simplest would be to add a verifyClosing boolean field in your form class with an initial value of true.

Whenever you want the form to close without confirmation then simply set verifyClosing = false before calling the Close method. Your closing event handler can check this value to decide what to do.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top