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