Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with ErrorProvider.Clear()

Tags:

c#

winforms

I have one problem. I validate two texboxs. If texbox are not validate I show error message with error provider.

Situation :

tbAzetId.Text="string"; tbHeslo.Text=empty;

errorprovider show error message in tbHeslo, this is ok.

Then I write some text in tbHeslo, click on button but errorprovider is still show error message in tbHeslo. Where can be problem?

Code is here:

    private bool IsAzetIdValid()
    {
        if (tbAzetId.Text!=String.Empty && Regex.IsMatch(tbAzetId.Text, "[^a-zA-Z0-9]"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private bool IsHesloValid()
    {
        if (tbHeslo.Text !=String.Empty)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private void btnPrihlasenie_Click(object sender, EventArgs e)
    {
        errorProvider.Clear();

        if (!IsAzetIdValid())
            errorProvider.SetError(tbAzetId, @"Nezadali ste Azet ID");
        else if (!IsHesloValid())
            errorProvider.SetError(tbHeslo, @"Nezadali ste heslo");
        else
            Text = "OK";
    }
like image 815
John Avatar asked Oct 04 '10 20:10

John


1 Answers

You'll need to clear the error provider text for that specific control when the error is cleared:

errorProvider.SetError(tbAzetId, "");
if (!IsAzetIdValid())
    errorProvider.SetError(tbAzetId, @"Nezadali ste Azet ID");

errorProvider.SetError(tbHelso, "");
if (!IsHesloValid())
    errorProvider.SetError(tbHeslo, @"Nezadali ste heslo");;

ErrorProvider.Clear is not enough:

To clear the error message, call the SetError method and pass in Empty for the String value.

like image 83
Michael Petrotta Avatar answered Oct 12 '22 00:10

Michael Petrotta