Confirmation of form closing

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
I want to do something like when click on the close button, a message box will pop up and if yes, close it else remain in the same form. But the following coding seems only consider one side while the other side is ignored.

Style = MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Question Or MsgBoxStyle.YesNo
Check = MsgBox("Do you want to quit?", Style, "Exit confirmation")
If Check = MsgBoxResult.Yes Then
Application.Exit()
End If
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Closing([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ComponentModel.CancelEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Closing
[/SIZE][SIZE=2][COLOR=#0000ff]      If[/COLOR][/SIZE][SIZE=2] MessageBox.Show("Do you want to exit?", "prompt", MessageBoxButtons.YesNo) = DialogResult.Yes [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]         Application.Exit()
[/SIZE][SIZE=2][COLOR=#0000ff]      Else
[/COLOR][/SIZE][SIZE=2]         e.Cancel = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]      End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Hello, I am trying to get my exit button to do the same thing.. Here is what i have. Notice most is from what you have supplied.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If MessageBox.Show("Do you want to exit?", "prompt", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Application.Exit()
Else
e.Cancel = True
End If

End

End Sub
 
Hello, I am trying to get my exit button to do the same thing.. Here is what i have. Notice most is from what you have supplied.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If MessageBox.Show("Do you want to exit?", "prompt", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Application.Exit()
Else
e.Cancel = True
End If

End

End Sub
That's not going to work because Cancel isn't a member of e for button clicks. Here they're using the FormClosing event and it's rather dated now, here's a better example:
VB.NET:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    'If the user's forcing it closed via task manager, or windows is shutting down: do not prompt
    If e.CloseReason <> CloseReason.TaskManagerClosing AndAlso e.CloseReason <> CloseReason.WindowsShutDown Then
        'Cancel only if the user selects No, otherwise it doesn't matter, the form's closing
        'No need for Application.Exit... ever
        e.Cancel = MessageBox.Show("Do you want to exit?", "prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No
    End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Just a button, just call Me.Close, the FormClosing event takes care of the rest
    Me.Close()
End Sub
 
Back
Top