views:

231

answers:

1

I have a code snippet that I want to run when the app is closing. So, I used FormCLosing event. But now i wanna place a confirmation message for exiting. Like, if the user clicks the Exit(X) button, there'll be a prompt, if he clicks NO, then the app will not close and revert to previous state.

Now I find that hard to achieve using FormClosing event. because it'll get executed no matter what button the user clicks. Any remedy for that?

I mean, I need an even like ExitButtonPressed()..

+3  A: 

You could try something like

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    If (MessageBox.Show("Close?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No) Then
        e.Cancel = True
    End If
End Sub

Have a look at

FormClosingEventArgs Class

And

CancelEventArgs.Cancel Property

The event can be canceled by setting the Cancel property to true.

astander
Inside the FormClosing event.
Aseem Gautam
Yes, sorry, changed that X-)
astander
Thanx. but having another problem now. http://stackoverflow.com/questions/2256909/vb-net-messagebox-with-yesnocancel-no-cancel-triggers-same-event
Bibhas