Preventing a VB.Net form from closing
Question
We are using this coding to handle the clicking of the big red X as a means to bypass all textbox validation on the form.
The code will test if any changes are made to the data bound controls on the form. The code handle cancelling changes made prior to closing the form.
Would would also like to cancel the clicking of the big X and not allow the form to close.
Can you show any needed coding that will not allow the form to actually close? We would like to add this new coding after the Else statement in the coding show below.
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case ((m.WParam.ToInt64() And &HFFFF) And &HFFF0)
Case &HF060 ' The user chose to close the form.
Me.StudentsBindingSource.EndEdit()
Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable
If Me.StudentsDataSet.HasChanges Then
' Alert the user.
'----------------
If MessageBox.Show("You are about to loose any *** Student *** changes you have made! " & vbCrLf & vbCrLf & _
"ARE YOU SURE YOU WANT TO DO THIS?" & vbCrLf & vbCrLf, _
"*** W A R N I N G ***", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
RibbonButtonCancelChanges_Click(Nothing, Nothing)
Else
' Reset validation.
'------------------
Me.CausesValidation = True
End If
End If
End Select
MyBase.WndProc(m)
End Sub
We tried this but the Validating event of the textbox controls execute which is not what we want.
Private Sub FormStudents_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable
Me.StudentsBindingSource.EndEdit()
If Me.StudentsDataSet.HasChanges Then
' Alert the user.
'----------------
If MessageBox.Show("You are about to loose any *** Student *** changes you have made! " & vbCrLf & vbCrLf & _
"ARE YOU SURE YOU WANT TO DO THIS?" & vbCrLf & vbCrLf, _
"*** W A R N I N G ***", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
RibbonButtonCancelChanges_Click(Nothing, Nothing)
Else
' Reset validation.
'------------------
Me.CausesValidation = True
e.Cancel = True
End If
End If
End Sub
Solution
You shouldn't be using WndProc
at all.
Instead, handle the FormClosing
event and set e.Cancel
to true.
OTHER TIPS
As SLaks said, you should use the FormClosing() event procedure instead of introducing complexity with WndPorc(). Overriding WndProc() is used in languages like C++ where you don't have the luxury of an event procedure to handle these events. But the simplicity of VB.NET provides you with an event procedure called FormClosing(). Just open your code and select your form name in the object dropdown (on left), and select FormClosing from the events dropdown (on right). This should give you a template to write your event code, something like this:
Private Sub FormClosing(Source as Object, e as EventArgs) Handles MyForm.Closing
e.Cancel = True
End Sub
Just add "e.Cancel = True" as shown above, and the form will never close!
Thanks for letting me know about FormClosing and e.Cancel
I was able use a combination of FormClosing and WndProc that handles everything we need.
I added this right after the form class name:
Dim blneCancel As Boolean = False
My WndProc now looks like this. Notice the setting of blneCancel.
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case ((m.WParam.ToInt64() And &HFFFF) And &HFFF0)
Case &HF060 ' The user chose to close the form.
Me.StudentsBindingSource.EndEdit()
Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable
If Me.StudentsDataSet.HasChanges Then
' Alert the user.
'----------------
If MessageBox.Show("You are about to loose any *** Student *** changes you have made! " & vbCrLf & vbCrLf & _
"ARE YOU SURE YOU WANT TO DO THIS?" & vbCrLf & vbCrLf, _
"*** W A R N I N G ***", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
RibbonButtonCancelChanges_Click(Nothing, Nothing)
Else
' Reset validation.
'------------------
Me.CausesValidation = True
blneCancel = True
End If
End If
End Select
MyBase.WndProc(m)
End Sub
The FormClosing procedure looks like this:
Private Sub FormStudents_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If blneCancel = True Then
e.Cancel = True
End If
End Sub
Now the user can type anything in the phone number textbox and it won't validate if the user clicks the big X to close the form. The form will just show the message warning the user something has changed and give them the choice to get back and attempt to save the changes or just exit without saving anything.