Reputation: 131
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";
}
Upvotes: 6
Views: 24659
Reputation: 39
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().
Upvotes: 1
Reputation: 152
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");
}
}
Upvotes: 2
Reputation: 2294
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
Upvotes: -1
Reputation: 21
Using multiple ErrorProvider objects will cause this behaviour. My solution was to just use one ErrorProvider.
Upvotes: 2
Reputation: 536
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();
Upvotes: 1
Reputation: 185
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.");
}
}
Upvotes: 4
Reputation: 10237
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();
Upvotes: 0
Reputation: 60902
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.
Upvotes: 14
Reputation: 10650
Use errorProvider.SetError(ctlName, "") to clear the error message from a control.
Upvotes: 5