R – causing validation on the textbox

custom-controlsvalidationwinforms

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.

Best Answer

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.