Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
private void tbxWidthBackIamge_Validated(object sender, EventArgs e)
{
    this.errBackWidth.SetError(tbxWidthBackIamge, "Enter the Width");
    if (tbxBackImage.Text != "")
        this.errBackHeight.Dispose();
}


I use an errorProvider to validate a textbox. It appears when there occurred an error. But It didn't disappear after correct the error. Is any error in this code?
Posted
Updated 23-Aug-11 18:06pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Aug-11 0:30am    
Why disposing something on invalid case?
--SA

First of you should really use the Validating event. Second you don't validate anything you just set an error message and dispose something else.

So

  1. Use the Validating event
  2. Use SetError(..., "ErrorMessage") when the input is incorrect
  3. Use SetError(..., string.Empty) when the input is correct
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Aug-11 0:30am    
Correct, my 5. I started to edit my answer at the same time; put some more detail, please see.
--SA
Jeremy Shin Woo 24-Aug-11 1:23am    
Thanks Simon..
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:

C#
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:

C#
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:
C#
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
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 24-Aug-11 0:44am    
Very good answer +5
Sergey Alexandrovich Kryukov 24-Aug-11 0:46am    
Thank you, Simon.
--SA
Jeremy Shin Woo 24-Aug-11 1:07am    
Thanks for ur kindness. :)
Sergey Alexandrovich Kryukov 24-Aug-11 1:10am    
You're very welcome; hope it helps you.
If you agree it makes sense, please formally accept the solution (green button) -- thank you.
--SA
Jeremy Shin Woo 24-Aug-11 1:22am    
Already I have done it. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900