views:

302

answers:

2

Hi guys,

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 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

+1  A: 

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
tommieb75
thanks a lot...it works...but on a second click....as soon as i click the close button it shows the error and on a second click it allows to close the form.........is there any way to prevent it???
Pankaj Kumar
@Pankaj: Handle the Form.Close event and set the flag in there also...you may have to add an override by subclassing the form...I will amend this answer to illustrate this....
tommieb75
thanks for taking the time to answer.i will be using nobugz answer as the accepted solution.
Pankaj Kumar
+1  A: 

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;
}
Hans Passant
@nobugz: thank you very much. it is working flawlessly
Pankaj Kumar