Perrimanzon
Perrimanzon

Reputation: 35

Form_Closing event in vb.net launched twice

I'm developing a simple app in vb.net (vs2010). I'm using the Form_Closing event in the following way:

Private Sub frmPrincipal_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If MessageBox.Show("Quit", "Are you sure?", _
           MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
         Me.Close()
    Else
         e.Cancel = True
    End If
End Sub

This code shows a dialog asking the user if he really wants to leave the app. I the answer is 'No', it works fine, but if the answer is 'Yes', it seems that the event Form_Closing is launched again, and the same dialog is showed again, asking if the user really wants to exit the app... The answer in this second dialog is not evaluated by the app, this means, the app will end after this second dialog.

My question is, how to avoid this second dialog when the user select 'Yes' on it?

Regards, Daniel.

Upvotes: 1

Views: 383

Answers (2)

Amit Saraf
Amit Saraf

Reputation: 131

You have already Closed the Form and again Calling the same pressing Yes so in place of

Me.Close

Use

End 

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190945

You shouldn't need to call Close() a second time.

Upvotes: 3

Related Questions