|
-
Jul 28th, 2008, 04:28 AM
#1
Thread Starter
Lively Member
[RESOLVED] How to stop closing form?
I need to check whether the user really want to close the form.
But if I do this in Formclosing, the form will be closed anyway.
-
Jul 28th, 2008, 05:47 AM
#2
Addicted Member
Re: How to stop closing form?
You can use e.Cancel to close or cancel closing the form.
vb.net Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MsgBox("Are you sure you want to close?", MsgBoxStyle.YesNo, "Close?") = MsgBoxResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
-
Jul 28th, 2008, 07:10 AM
#3
Re: How to stop closing form?
This question has already been answered nicely by Link, though this sample code uses the preferred .Net framework System.Windows.Forms namespace version of the message box as opposed to the Microsoft.VisualBasic.Interaction namespace version:
Code:
Private Sub Form1_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to close?", "Confirm form closure", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
e.Cancel = False
Case Else
e.Cancel = True
End Select
End Sub
-
Jul 28th, 2008, 07:42 AM
#4
Re: How to stop closing form?
To summarize it all on one line:
Code:
Private Sub Form1_FormClosing(...) Handles Me.FormClosing
e.Cancel = MessageBox.Show("Are you sure you want to close?", "Confirm form closure", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|