Problem with ErrorProvider.Clear()
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;
}
}
pr开发者_StackOverflowivate 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";
}
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.
Use errorProvider.SetError(ctlName, "") to clear the error message from a control.
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.");
}
}
Using multiple ErrorProvider objects will cause this behaviour. My solution was to just use one ErrorProvider.
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");
}
}
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();
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();
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
精彩评论