How to code the close button in the form?

annir

Active member
Joined
Sep 1, 2008
Messages
32
Programming Experience
Beginner
Hi Guys,

Can I ask if someone have an idea on how can I write a code in the close button in the form.

Example, if the user click the close button (the red box with X) then a message box will appear if he clicks yes then it will end, if cancel the form will not close.

Thanks!

annir
 
put this script to your close button (when you click on the close button in your form)

VB.NET:
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Dim response As MsgBoxResult
response = MsgBox("Do you want to close form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
Exit Sub
End If
End Sub

put this script to your form closing (when you click on the X button on top)
VB.NET:
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox("Do you want to close?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Exit Sub
End If
End Sub

hope this helps you.
 
put this script to your close button (when you click on the close button in your form)

VB.NET:
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Dim response As MsgBoxResult
response = MsgBox("Do you want to close form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
Exit Sub
End If
End Sub

put this script to your form closing (when you click on the X button on top)
VB.NET:
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox("Do you want to close?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Exit Sub
End If
End Sub

hope this helps you.
Wow, just look at all of that un-needed redundancy...

To close the form:
VB.NET:
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
    Me.Close()
End Sub
For confirmation:
VB.NET:
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = (MessageBox.Show("Confirm close?", "Closing Window", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel)
End Sub
 
Hi

Hi Guys,

Can I ask if someone have an idea on how can I write a code in the close button in the form.

Example, if the user click the close button (the red box with X) then a message box will appear if he clicks yes then it will end, if cancel the form will not close.

Thanks!

annir

:D
This code i use in vb 2008
'===================================================
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox("Are you sure you want to close the application?", vbYesNo + vbExclamation + vbApplicationModal + vbDefaultButton2, "Close confirmation")
If response = MsgBoxResult.Yes Then
Me.Close()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Exit Sub
End If
End Sub
'========================================
 
:D
This code i use in vb 2008
'===================================================
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox("Are you sure you want to close the application?", vbYesNo + vbExclamation + vbApplicationModal + vbDefaultButton2, "Close confirmation")
If response = MsgBoxResult.Yes Then
Me.Close()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Exit Sub
End If
End Sub
'========================================
Here's something a little cleaner:
VB.NET:
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = MessageBox.Show("Are you sure you want to close the application?", "Close confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) <> Windows.Forms.DialogResult.OK
    End Sub
 
Back
Top