Click here to Skip to main content
15,914,368 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,

I have a situation here where in form 1, I have 10 textbox and in form 2, I have 15 textbox that must be filled before try to save.

If I use the "If txt1.text<>string.empty OR txt2.text<>string.empty then", my checking statement must be quite long..

So does anyone here teach me the simplest way to check all textbox in a form? I guess there must be a way to check by loop the system.windows.textbox but I don't have any idea how..

Kindly please show me..tq
Posted
Comments
Tosby Odhiambo Joséph 2-Nov-16 8:57am    
I have the same situation only that in my case I also have two group boxes that group together the text boxes into two groups and i want to check for empty fields as all at once. Is there a shorter way that I can do that?

First of all, if you have 10 text boxes, it's already a good reason to avoid adding them using the Designer. Designer often means boring unproductive manual work which could be done in code much faster with much better quality, especially in terms of maintenance. If you do that, you problem would be reduced to:
C#
TextBox[] textBoxes = new TextBox[textBoxCount];
// create, arrange them in its parent control using parent.Controls.Add(/* ... */);

//... and then use this repeatedly:

foreach (TextBox textBox in textBoxes) { /* ... */ }


There is more "clever" and complex way which would work even if you don't have an array or other collections with your text boxes: you can create a loop based on Control.Controls, iterate some top control (Form, for example) through parent, and identify text boxes using:
C#
void ProcessTextBoxes(Control parent) {
   foreach(Control child in parent.Controls) {
      TextBox textBox = child as TextBox;
      if (textBox != null) // type match
          ProcessTextBox(textBox); //define some method to work with individual text box
      else
          ProcessTextBoxes(parent); //recursive
   } //loop
}


You can do it all, but why? Not creating them with Designer is much easier and better for maintenance.

Finally, I am not that you need to check the text boxes for empty text at all. Maybe you need to handle the event TextBox.TextChanged to detect that the user ever changed the text. This is how some command like "Save" gets enabled. Also, you can check up the property TextBox.Modified. These methods can help you to handle the situation when the user entered the valid empty text. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.modified.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx[^].

Even if you really need to check up the text values, in more general cases you need to validate the values; an validation can be more complex then a check for emptiness. (By the way, chances are, you need to use string.Trim before this check. The validation is the regular holistic concept you should better get familiar with:
http://msdn.microsoft.com/en-us/library/ms229603.aspx[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
fjdiewornncalwe 28-Sep-12 22:18pm    
+5. As an addition, my personal approach to something like this in the past was to tie each of the textboxes to a single onchange event which would maintain the state of the "submit" button so that until all textboxes are in appropriately filled in, the submit button remains disabled.
Sergey Alexandrovich Kryukov 28-Sep-12 22:53pm    
Quite reasonable. It depends on how "individual" those controls are, though.
Thank you, Marcus.
--SA
Abhinav S 28-Sep-12 22:53pm    
Perfect a 5.
Sergey Alexandrovich Kryukov 28-Sep-12 22:53pm    
Thank you very much, Abhinav.
--SA
VB
Dim EmptyTextBoxFound As Boolean = False 'Boolean flag for empty textbox 
Dim EmptyTextBoxName As String = ""
For Each ctl As Control in Me.Controls

If TypeOf ctl Is TextBox And ctl.Text.Length = 0 Then 
EmptyTextBoxName = ctl.Name
EmptyTextBoxFound = True
Exit For 
End For 
If EmptyTextBoxFound = True  Then 
'.. do whatever you have do
 End If 
 
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