Data validation in WinForms with ErrorProvider component

I'm new to WinForms and have a basic form where the user enters data into a TextBox and can click "save" or "cancel". I would like to perform data validation so that the user actually entered something into the TextBox, but at the moment the ErrorProvider icon only appears if the TextBox itself is clicked and does not validate when the Save button is clicked as I intended. I am also using Visual Studio 2013.

This event handler code is in the method btnSave_Click

:

this.txtNote.Validating += new
        System.ComponentModel.CancelEventHandler(this.txtNote_Validating);

      

And the method txtNote_Validating

looks like this:

protected void txtNote_Validating(object sender,
                         System.ComponentModel.CancelEventArgs e)
    {
        if(txtNote.Text.Length == 0)
        {
            errorProvider1.SetError((Control)txtNote, "An explanation of your time entry is required.");
        }
        else
        {
            errorProvider1.SetError(txtNote, "");
        }
    }

      

Also, I haven't added any events to the properties window for mine txtNote

, and HAVE added a check event: txtNote_Validating

and Click: btnSave_Click

to my btnSave events in the properties window.

My question is why my TextBox only validates when I click on it, and how can I make my Save button validate the TextBox's validation using the ErrorProvider component?

+3


source to share


1 answer


I think the whole concept of validation is crap. Why do I want to check the value TextBox

when focus is lost? I want to test it as user text input or at the end when the user clicks a button Okto check the cross dependencies of the entered values.

Specifically in your case: validation won't happen because you can click the button Savewithout typing anything into TextBox

. Or it will happen when you click a button Cancelwhile in the middle of typing a value in TextBox

, which is silly since you just want to close the dumb form;)

There are two approaches to verification:

  • instantly, during editing, preventing user access to invalid commands;
  • final to discreetly reject the user's command.

First, we basically monitor the user input all the time and keep the button Savedisabled until all fields are filled in and the values ​​are in order. The second save button is Saveon all the time, but as soon as the user clicks it, everything will be checked, and if something is invalid, nothing will happen.

Both approaches can use hints to tell the user what is wrong. In your case ErrorProvider

.



Let the second approach be applied

void buttonSave_Clicked(object sender, EventArgs e)
{
    if(string.IsNullOfEmpty(txtNote))
    {
        errorProvider1.SetError(txtNote, "Omg, can't haz empty note");
        return;
    }
    if(string.IsNullOfEmpty(someOtherTextBox))
    {
        errorProvider1.SetError(someOtherTextBox, "Omg, no empty plx!");
        return;
    }
    // 
    ...
}

      

Here we check the values ​​one by one. The first one in order will trigger the install ErrorProvider

and the function will exit. Can also show ErrorProvider

for all invalid controls:

void buttonSave_Clicked(object sender, EventArgs e)
{
    bool isOk = true;
    if(string.IsNullOfEmpty(txtNote))
    {
        errorProvider1.SetError(txtNote, "Omg, can't haz empty note");
        isOk = false;
    }
    if(string.IsNullOfEmpty(someOtherTextBox))
    {
        errorProvider1.SetError(someOtherTextBox, "Omg, no empty plx!");
        isOk = false;
    }
    // 
    if(isOk)
    {
        ...
    }
}

      

One problem is that ErrorProvider

, once installed, it will keep blinking forever (or until you correct the error and press the button again). A simple workaround is to use Timer

that will turn the error off after a few seconds. Once you set the timer to start the error when the time expires:

private void timerError_Tick(object sender, EventArgs e)
{
    timerError.Stop();
    errorProvider1.Clear();
}

      

+4


source







All Articles