Problem with FormClosing event
-
Hi Guys, Every time I try to verify if the user wants to exit the app, the question box comes out twice. I do it like this:
Private Sub Register_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing Dim exitChoice As Windows.Forms.DialogResult exitChoice = MessageBox.Show("Are you sure you want to exit?", "Exiting?", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) If exitChoice = Windows.Forms.DialogResult.Yes Then Application.Exit() Else e.Cancel = True End If End Sub
It comes out twice even if yes is clicked the first time. How do i make it come out only once?He who goes for revenge must first dig two graves.
-
Hi Guys, Every time I try to verify if the user wants to exit the app, the question box comes out twice. I do it like this:
Private Sub Register_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing Dim exitChoice As Windows.Forms.DialogResult exitChoice = MessageBox.Show("Are you sure you want to exit?", "Exiting?", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) If exitChoice = Windows.Forms.DialogResult.Yes Then Application.Exit() Else e.Cancel = True End If End Sub
It comes out twice even if yes is clicked the first time. How do i make it come out only once?He who goes for revenge must first dig two graves.
The form is closing anyway so remove the application.exit (assuming this is the final form) Only do the cancel = true if they want to stay in. Private Sub Register_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing Dim exitChoice As Windows.Forms.DialogResult exitChoice = MessageBox.Show("Are you sure you want to exit?", "Exiting?", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) If exitChoice = Windows.Forms.DialogResult.No Then e.Cancel End If End Sub
-
The form is closing anyway so remove the application.exit (assuming this is the final form) Only do the cancel = true if they want to stay in. Private Sub Register_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing Dim exitChoice As Windows.Forms.DialogResult exitChoice = MessageBox.Show("Are you sure you want to exit?", "Exiting?", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) If exitChoice = Windows.Forms.DialogResult.No Then e.Cancel End If End Sub
' you need to add the following code to fix it ' on the same file where your Register_FormClosing is, declare this Private if_already_close As Integer ' inside your Register_FormClosing, add ' before Dim exitChoice If if_already_close <> 1234 Then if_already_close = 1234 ' your MessageBox.Show(...) codes here End If ' before the End Sub
TMALBONPH