Cancelling the Validating event


Hi, I have an MDI app that opens an MdiChild form where I have some TextBoxes and I run some Validating logic. I use the ErrorProvider control and the Event Validating over the TextBoxes. It all works fine, except that 'while' I still don´t have all my TextBoxes validated I just 'CAN´T' close the MdiChild form neither by clicking in the 'x' button or by adding an extra Cancel button to close the form.

Is there any solution for this

Regards


Answer this question

Cancelling the Validating event

  • leriksen71

    Lol... There are a couple of bugs floating around, as with any software, so don't get too discouraged. :)  I know that the Windows Forms team is aware of the Validating bug, but I don't know if they fixed it for version 1.1.  I can work with you to get around the issue if you like, as I have already been down this path.  My situation was specifically with a textbox.  The textbox was in a TabPage which was in a TabControl which was on a Form.  The check I did looked like this in the ValidatingEvent:

    Dim st As New StackTrace()
    Dim sf As StackFrame = st.GetFrame(5)
    If sf.GetMethod.Name <> "Validate" Then
        'ValidationCode Here
    End If


    The important but is the parameter for the GetFrame call.  To get the number, identify a control that demonstrates the problem, count the containers up to the form and add 2.  That should get you there (if you are dealing with an MDI app, you may need to add 3 due to "secret" containers.)

    Let me know if this helps or if you have any questions.

  • rohit_singh1679

    Jacob

    I implemented this code, but it appears that I am getting the full-blown version of this bug (or something).

    I have two examples of the problem, both to do with dialog boxes being opened as a result of the validating event.

    Example 1:

    When clicking off an MDI Child or Dialog form (say, into the MDI Parent) from a control which doesn't have valid data in it, the dialog opens, but when it is closed (either as a result of the OK or Cancel buttons), I get an SEHException.  I have a handler for the Application.ThreadException event.

    Example 2:

    When pressing Cancel whilst a control with invalid data in it has the focus, the validation event fires (dialog box), however this causes the SEHException event.

    Ideas and insights would be gratefully received.

  • Christopher Smith

    You're encountering a bug.  For some reason, forms fire two validating events when CausesValidation property is set to true.  Setting the CausesValidation property to false only supresses one of the Validation events.  The only work around I have come up with is a complete and total hack that involves inspecting the stack at runtime for a particular method call and ignoring the validating logic if that method call exists.
  • Todd Kitta

    Hi Jacob,

    I am guessing that this bug is basic to the framework, i.e., appears in C# code also. Correct  I am running vers 1.1 (2003) and it is still with us. Is there a fix yet

    I have implemented a form using a TabControl with 5 TabPages. On one page, I have a number of controls, i.e., ComboBoxes, RadioButtons, NumericUpDowns, etc. I want to validate the data entered by the user into these controls and at the same time allow the user to cancel the form if he wishes. To implement this, I copied some sample code:

    private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
            tabControl1.TabPages[tabControl1.SelectedIndex].Focus(); 
            tabControl1.TabPages[tabControl1.SelectedIndex].CausesValidation = true; 
    }

    private void tpSchedule_Validating(object sender, CancelEventArgs e)
    {
           if ((string)cboSchedule.SelectedItem == "Weekly")
          {
                if (cboRunOnDOW.SelectedItem.ToString() == "")
    e.Cancel = true;
          }
    }

    My problem is that the TabPage always has the focus and won't allow the Cancel button push to be handled. If I remove the first line of the SelectedIndexChanged() event handler, the Cancel button push is handled, but then the validation stuff is never invoked. My workaround is at the top of the Validating event handlers, I determine if the Cancel button has the focus and if so, set the e.Cancel property to false, allowing the form to be cancelled.

    Thanks,
    Royce

  • Dale


    Oh, this is my first WindowsFormApp and I´m dacing a bug. What a great start, heheh

  • Cancelling the Validating event