Problem with ErrorProvider.Clear()
Asked Answered
C

9

6

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";
    }
Charmaincharmaine answered 4/10, 2010 at 20:32 Comment(0)
H
14

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.

Hemipode answered 4/10, 2010 at 20:36 Comment(2)
@John: It does work. If you're having problems with it, you'll need to be more specific. You may want to try just the error clearing and setting in a separate test function or app, to try to isolate the problem.Hemipode
To add to @MichaelPetrotta's answer here that SetError() with an empty string is the solution (also works for me), Clear() actually clears the settings for the error provider and is not the right approach as several people suggest in this thread. If you have set up your icon, for example, to appear on the MiddleLeft, calling Clear() will reset the ErrorIconAlignment to MiddleRight. Hardly desirable if just wanting to remove the error message!Inelastic
S
5

Use errorProvider.SetError(ctlName, "") to clear the error message from a control.

Skirling answered 4/10, 2010 at 20:37 Comment(0)
A
4

It is my experience that both

errorProvider.SetError(<ctrlName>, "");

and

errorProvider.Clear();

will remove the icon from the form. Be mindful of what ErrorProvider instance you are clearing. The example below works. However, if you move the ErrorProvider declaration inside the Validating Event, it will compile, create the error, but will not clear it.

ErrorProvider ep = new ErrorProvider();
private void txtBox_Validating(object sender, CancelEventArgs e)
{
    bool bValidated = double.TryParse(txtBox.Text, out txtBoxVar);
    if (bValidated)
    {
        ep.SetError(txtBox, String.Empty);
        ep.Clear();
    }
    else
    {
        ep.SetError(txtBox, "Enter a valid decimal.");
    }
}
Assurgent answered 3/1, 2013 at 20:42 Comment(0)
D
2

Using multiple ErrorProvider objects will cause this behaviour. My solution was to just use one ErrorProvider.

Decahedron answered 19/6, 2015 at 22:38 Comment(1)
This helped me alot after trying all solutions. My advice is to declare only one instance of errorprovider.Schnauzer
A
2

ErrorProvider.Clear will reset any settings that you have with the ErrorProvider such as Alignment, Padding, DataSource etc. while if you just want to clear it off the control (once it is validated correctly) then use the SetError(Control, ""). For your code, it would be:

private void btnPrihlasenie_Click(object sender, EventArgs e)
{
    if (IsAzetIdValid()) {
        errorProvider.SetError(tbAzetId, "");
    } else {
        errorProvider.SetError(tbAzetId, @"Nezadali ste Azet ID");
    }
}
Almsman answered 8/4, 2019 at 4:39 Comment(0)
T
1

I have never had to use errorProvider.Clear(), but I guess it depends on the settings you have altered (Clear() just resets the settings of the actual control not the errors). I've never wanted to reset my settings.

I have seen ideas such as looping through every control and setting the message to empty..

foreach (Control cr in this.Parent.Controls)
{
  errorProvider1.SetError(cr, "");
}

But for me, what actually worked great, was to just

errorProvider1.Dispose();
Tamis answered 23/5, 2015 at 4:8 Comment(0)
A
1

What I have discovered is that there are cases where Clear() by itself is insufficient. In particular, if the ErrorProvider has a custom class object assigned to it's DataSource, the DataSource will require being assigned again following Clear(). The same is likely to be true if a DataTable is assigned to the DataSource.

Regarding clearing an error using SetError(, ""). This does not clear the HasErrors() condition for the ErrorProvider. Only Clear() will do that. Even Null can be used instead of String.Empty without clearing HasErrors().

Athos answered 2/8 at 15:4 Comment(0)
K
0

errorProvider.SetError(<ctrlName>, "") simply sets the err msg to an empty string. To get rid of the error indicator entirely, you must call errorProvider.Clear();

Kyliekylila answered 7/6, 2012 at 21:2 Comment(1)
Calling SetError() with an empty string clears the error indicator for me. Note my comment above as well, where calling Clear() clears all the settings (as stated in the documentation).Inelastic
B
-1

Here is my solution to validate empty controls

1- Add new class to your project and create following method as shown below:

public class ValidationHelper
{
    private static ErrorProvider errProvider = new ErrorProvider();
    public static void ValidateFields(List<Control> controls)
    {
        errProvider.Clear();
        foreach (Control c in controls)
        {
            errProvider.SetError(c, "");
            if (string.IsNullOrWhiteSpace(c.Text))
            {
                errProvider.SetError(c, "Please fill the required field");
            }
        }
    }

}

2- and here is how to use my class above

    private void cmdSave_Click(object sender, EventArgs e)
    {
        try
        {
            List<Control> controls = new List<Control>();
            controls.Add(this.txtQty);
            controls.Add(this.txtComment);

            ValidationHelper.ValidateFields(controls);
            //rest of your code

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Note: We need to define/use one errorProvider. Thanks

Baden answered 21/8, 2017 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.