Check for Empty TextBox Controls in VB.NET

I got a Form application in VB.NET.

I have many text fields in one form (about 20). Is there anyway to check them all at once to find out if they are empty, and not write out a massive line of code to check each of them separately, for example

If txt1.text = "" Or txt2.text="" Then msgbox("Please fill in all boxes") 

Does this sound like a long way around him?

+8
winforms textbox
source share
7 answers

You can also use LINQ:

 Dim empty = Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0) If empty.Any Then MessageBox.Show(String.Format("Please fill following textboxes: {0}", String.Join(",", empty.Select(Function(txt) txt.Name)))) End If 

An interesting method is Enumerable.OfType

The same thing in the query syntax (more readable in VB.NET):

 Dim emptyTextBoxes = From txt In Me.Controls.OfType(Of TextBox)() Where txt.Text.Length = 0 Select txt.Name If emptyTextBoxes.Any Then MessageBox.Show(String.Format("Please fill following textboxes: {0}", String.Join(",", emptyTextBoxes))) End If 
+15
source share

A very simple approach is to collect all the TextBox controls in sequence using the Enumerable.OfType LINQ method, and then repeat it in for each loop :

 Dim textBoxes = Me.Controls.OfType(Of TextBox); For Each t In textBoxes If String.IsNullOrEmpty(t.Text) Then MsgBox("...") Exit For End If Next t 
+5
source share

I would recommend using the Validating event of the TextBox controls with the error provider control (just add it to your form):

 Private Sub TextBox_Validating( sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating, TextBox2.Validating, ComboBox1.Validating Dim ctl As Control = CType(sender, Control) If ctl.Text = "" e.Cancel = True ErrorProvider1.SetError(ctl,"Please enter a value") End If End Sub 

Then you can just call:

 ErrorProvider1.Clear() If Me.ValidateChildren() ' continue on End If 

The best part is that the user is informed about which text field is missing and required. This works with controls other than text fields, so you can provide a more complete solution. Also, if you go to a later point when one or two text fields don't need values, you just don't check them, and don't add special cases to your loops.

Finally, if you do not want to enter all the controls, you can do this in the form of load:

 For Each c As Control In Me.Controls If TypeOf(c) is TextBox or TypeOf(c) is ComboBox AddHandler c.Validating, AddressOf Me.TextBox_Validating End If Next 
+4
source share

If the TextBox empty, a window appears with the message "Complete Entry!"

 Dim t For Each t In Me.Controls If TypeOf t Is TextBox Then If t.Text = "" Then MsgBox("Complete Entry!") Exit Sub Exit For End If End If Next 
+1
source share

Sub to check Empty text box in GroupBox, you can use this:

 Public Sub CheckEmptyTextbox(Byval groupbox as GroupBox) Dim txt as control For Each txt in groupbox.Controls IF TypeOF txt is Textbox then IF txt.Text="" Then MsgBox("Please Input data to textbox.") Exit For End IF End IF Loop End Sub 
+1
source share

I found this, maybe you can change it to check if all text fields are clear, and not what it is doing now, which just clears all text fields

 Public Sub ClearTextBox(ByVal root As Control) For Each ctrl As Control In root.Controls ClearTextBox(ctrl) If TypeOf ctrl Is TextBox Then CType(ctrl, TextBox).Text = String.Empty End If Next ctrl End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ClearTextBox(Me) End Sub 
0
source share

Freestyle Open Class

 Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged If Trim(TextBox3.Text) = "" And Me.Visible Then MsgBox("fill in the textbox") TextBox3.BackColor = Color.Yellow Else ' MsgBox("great one !!!") End If End Sub Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged If Trim(TextBox2.Text) = "" And Me.Visible Then MsgBox("fill in the textbox") TextBox2.BackColor = Color.Yellow Else 'MsgBox("great one !!!") End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Trim(TextBox1.Text) = "" Or Trim(TextBox2.Text) = "" Or Trim(TextBox3.Text) = "" And Me.Visible Then MsgBox("Please fill the necesary", MsgBoxStyle.Critical, "Error") TextBox1.Focus() Else MsgBox("Nice Work !!!") End If End Sub Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave If Trim(TextBox1.Text) = "" And Me.Visible Then MsgBox("fill in the textbox") TextBox1.BackColor = Color.Yellow Else ' MsgBox("great one !!!") End If End Sub 

Final class

-one
source share

All Articles