Problem with ErrorProvider.Clear ()

I have one problem. I check two texbox. If texbox is not checked, I display an error message with the error provider.

Situation:

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

errorprovider show error message in tbHeslo, this is normal.

Then I write the text in tbHeslo, click the button, but errorprovider still shows the error message in tbHeslo. Where could the problem be?

The 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"; } 
+4
source share
7 answers

You need to clear the error provider text for this specific control if 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 String to Null.

+11
source

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

+3
source

In my experience, both

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

and

errorProvider.Clear();

will remove the icon from the form. Remember which instance of ErrorProvider you are ErrorProvider . An example is given below. However, if you move the ErrorProvider inside the Validating Event, it ErrorProvider , generates an error, but does 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."); } } 
+2
source

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

I saw ideas like looping through each control and setting the message to empty.

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

But for me, what really worked great was just

 errorProvider1.Dispose(); 
+1
source

Using multiple ErrorProvider objects will result in this behavior. My solution was to just use one ErrorProvider.

+1
source

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

-1
source

Here is my solution for checking for empty controls

1- Add a new class to your project and create the 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's 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

-1
source

All Articles