views:

120

answers:

2

I made a custom text box yesterday with its own validator:

public partial class CustomTextBox : TextBox
{
    ErrorProvider errorProvider;

    public CustomTextBox()
    {            
        InitializeComponent();
        errorProvider = new ErrorProvider();
        errorProvider.DataSource = this;
    }        


    protected override void OnValidating(CancelEventArgs e)
    {
        base.OnValidating(e);
        if (this.Text.Trim() == "")
        {
            errorProvider.SetError(this, "Required field");
            e.Cancel = true;
            return;
        }
        errorProvider.SetError(this, "");
    }


}

So I put this on a form with a cancel button and set causesValidation to false on the cancel button. I also set causesValidation on the form to false. For some reason if I click cancel my customtextbox is still firing the onValidating event. Any ideas what's causing this? I'd like to not have anything validate my text boxes until I click a submit button which will try to validate all controls on the form. This way a user doesn't get forced to enter data into the control before moving on to another. Sound reasonable? This is my first crack at a winforms UI.

A: 

Looks like it's a known issue: Visual Studio Feedback.

I set AutoValidate = AutoValidate.Disable; on the form, and it no longer validated. I'm not sure where you'd fit that into your requirement though.

nickd
A: 

I later found a simple property of the form called AutoValidate. I set it to EnableAllowFocusChange and now I have the exact behavior I want, the errorprovider will warn when a user leaves the control and validation fails, but the user is not restricted to the control.

Justin Holbrook