question

LanceJames-3930 avatar image
0 Votes"
LanceJames-3930 asked karenpayneoregon commented

Cannot get Control.CausesValidation = false; to work

I have the following TextBox Validating event (just test validation data entry to prove working method):

         private void txtPart_Validating(object sender, CancelEventArgs e)
         {
             string error = null;
             if (txtPart.Text != "L")
             {
                 error = "Validation failed - Part Number";
                 e.Cancel = true;
                 errorProvider2.SetError((Control)sender, error);
             }
         }

The Validation works fine. However, I have also included the following code during initialization to stop the Validation events if I click a Cancel button.

 this.btnCancel.CausesValidation = false;
 this.CausesValidation = false;

If I click the Cancel button it resets all TextBoxes to "" and then tries to set focus on the first TextBox. When I click the Cancel button the clearing of the TextBoxes happens but the Validation event is raised for the TextBox that has/had focus. I get trapped on the last TextBox.

Everything I have read indicates I am doing this correctly, but the reality is I must not be as the Cancel button does not work as intended.

Any guidance will be greatly appreciated.

Regards,
Lance

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

An alternate approach can be found here using data annotations.


0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered LanceJames-3930 edited

Try handling these events:

 private void btnCancel_Click( object sender, EventArgs e )
 {
    AutoValidate = AutoValidate.Disable;
    Close( );
 }
    
 private void Form1_FormClosing( object sender, FormClosingEventArgs e )
 {
    if( e.CloseReason == CloseReason.UserClosing && AutoValidate != AutoValidate.Disable )
    {
       AutoValidate = AutoValidate.Disable;
       BeginInvoke( (Action)Close );
    }
 }

Inside the FormClosing you can ask the user for confirmation.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I went back to testing with validation on all fields. The AutoValidate turns-off all validation for all fields even before I click the Cancel button. This is not the behavior I am seeking. I need the validation suppressed only when I click the Cancel button.

The Cancel button is to reset all textboxes to empty and place focus on the first textbox.

I need the validation to stay live until the Cancel is clicked.

The original code I posted is all over the internet including MS itself. However, the Control.CausesValidation doesn't stop the validation for the textbox that had focus when I clicked the Cancel button.

So, still needing help here.

Regards,
Lance

0 Votes 0 ·