You did not clear the error when the data is valid. You also call
SetError
with non-empty error message unconditionally. What's the criteria for validity? Non-empty value? Than call
SetError
depending on it:
private void tbxWidthBackIamge_Validated(object sender, EventArgs e) {
if (tbxBackImage.Text.Trim() == string.Empty)
this.errBackWidth.SetError(tbxWidthBackIamge, "Enter the Width");
else
this.errBackWidth.SetError(tbxWidthBackIamge, string.Empty);
}
Pay attention, I use
string.Empty
instead of "" (never do it). I also suggest to add
Trim
assuming that a string containing only blank space characters is also invalid. If my assumption is wrong, simply remove
Trim
.
Better yet, use anonymous delegate:
myControl.Validated += delegate(object sender, System.EventArgs eventArgs) {
if (tbxBackImage.Text.Trim() == string.Empty)
this.errBackWidth.SetError(tbxWidthBackIamge, "Enter the Width");
else
this.errBackWidth.SetError(tbxWidthBackIamge, string.Empty);
}
With C# v.3 or v.4 you could use the even better syntax with lambda form of the delegate:
myControl.Validated += (sender, eventArgs) => {
if (tbxBackImage.Text.Trim() == string.Empty)
this.errBackWidth.SetError(tbxWidthBackIamge, "Enter the Width");
else
this.errBackWidth.SetError(tbxWidthBackIamge, string.Empty);
}
See the code sample on this MSDN help page:
http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx[
^].
Does your code even compile? I mean, what is
errBackHeight
which you dispose. Does it implement
IDisposable
? Why disposing anything on error? Just checking.
—SA