Errorprovider shows error on using windows close button(X)
Is there any way to turn the damned error provider off when i try to close the form using the windows close button(X). It fires the validation and the user has to fill all the fields before he can close the form..this will be a usability issue because many tend to close the form using the (X) button.
i have placed a button for cancel with causes validation to false and it also fires a validation.
i found someone saying that if you use Form.Close() function validations are run... how can i get past this annoying feature.
i have a MDI sturucture and show the form using
CreateExam.MdiParent = Me
CreateExam.Show()
on the mdi parent's开发者_StackOverflow中文版 menuitem click
and have this as set validation
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If String.IsNullOrEmpty(TextBox1.Text) Then
Err.SetError(TextBox1, "required")
e.Cancel = True
End If
If TextBox1.Text.Contains("'") Then
Err.SetError(TextBox1, "Invalid Char")
e.Cancel = True
End If
End Sub
Any help is much appreciated. googling only showed results where users were having problem using a command button as close button and that too is causing problem in my case
The ValidateChildren() method prevents the form from closing. Paste this code in your form to fix that:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
}
This is quite simple fix, in your Form's Closing
Event, set a flag to indicate leaving the form, for example blnLeave
, when the Form gets loaded, set the flag to False
, when the Closing
event gets triggered, set that to True
within that event handler, then the change as follows would be
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating If (blnLeave) Then e.Cancel = False; Return End If If String.IsNullOrEmpty(TextBox1.Text) Then Err.SetError(TextBox1, "required") e.Cancel = True End If If TextBox1.Text.Contains("'") Then Err.SetError(TextBox1, "Invalid Char") e.Cancel = True End If End Sub
Edit: Amended this answer for inclusion as per OP's comments. My suggestion is to handle the Form's Closed Event as shown
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed blnLeave = True End Sub
And handle it here in the Form's window procedure override as shown here....
Private Const SC_CLOSE As Integer = &HF060 Private Const WM_MENUSELECT As Integer = &H11F Private Function LoWord(ByVal Num As Integer) As Integer LoWord = Num & &HFFFF End Function Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_MENUSELECT Then If LoWord(m.WParam.ToInt32()) = SC_CLOSE Then ' Handle the closing via system Menu blnLeave = True End If End If MyBase.WndProc(m) End Sub
精彩评论