Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Title say's it all. I've been doing it like this,

C#
if (string.IsNullOrWhiteSpace(textEdit1.Text))
                   {
                       XtraMessageBox.Show("Textbox can't be empty ", "Error"); return;

                   }


But I'm trying to clean up my coding and some options has 10 text boxes, so it looks really sloppy with that pasted 10 times haha.

What I have tried:

C#
if (string.IsNullOrWhiteSpace(textEdit1.Text))
                   {
                       XtraMessageBox.Show("Textbox can't be empty ", "Error"); return;

                   }
Posted
Updated 12-Dec-17 20:07pm
Comments
Bryian Tan 13-Dec-17 0:27am    
________________ 13-Dec-17 2:18am    
All simple - if you need same behavior from controls - best way is to create UserControl.
In code of UserControl - you once add all needed validations, than simple drag and drop this new element to your form from designer.
It is ability to add custom properties - so you could change the validation principal also from designer.

1 solution

I'd leave it as it is: maybe move the tests into a method that returns false if any box was empty but I'd leave it as separate tests - if only so that you can give an explicit reason and put the focus to the "faulty" input:
C#
if (string.IsNullOrWhitespace(tbAddress.text))
   {
   XtraMessageBox.Show("An address is required", "Error");
   tbAddress.Focus();
   return false;
   }
if (string.IsNullOrWhitespace(tbMobile.text))
   {
   XtraMessageBox.Show("An mobile number is required", "Error");
   tbMobile.Focus();
   return false;
   }
...
It looks clumsy as code, but it's a lot more friendly for the user.
 
Share this answer
 

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