Detecting if all textboxes have data in them

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Silly question

I think i seen a little function on here once that you could use to make sure textboxes had data in them before you allowed the program to continue.

Basically I have a number of textboxes, the information the the textboxes is to be sent to a database, but before this i want to make sure there is information in them. Anyone have a little function or piece of code that I could use, instead of a big if statement.

Oh and also if some checkedlistboxes have been checked.

Thanks
 
Last edited:
VB.NET:
Dim ctrl As Control
Dim bAllDataIsEntered As Boolean = True
For Each ctrl In Me.Controls
    If TypeOf ctrl Is TextBox Then
        'using Trim disAllows spaces as data
        If ctrl.Text.Trim = String.Empty Then
            bAllDataIsEntered = False
        End If
    ElseIf TypeOf ctrl Is CheckBox Then
        Dim chx As CheckBox = CType(ctrl, CheckBox)
        If Not chx.Checked Then
            bAllDataIsEntered = False
        End If
    End If
Next
If bAllDataIsEntered Then
    'update dataSource
    MessageBox.Show("All data has been entered.")
Else
    MessageBox.Show("All data has NOT been entered."), _
      "Enter the data!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
You could also add all the controls that you want to validate into a groupBox and only enumerate the controls in that groupBox using GroupBox.Controls.
 
Ok thats cool, is it a function? And where do I add the textboxes to validate. Also how does that groupbox work because the textboxes are in a groupbox so it would be great if you could show me how to do that.

Thanks
 
It's not quite a function, it's only sample code. To make it a function you would give a function declaration and return a value:
VB.NET:
Private Function AllDataEntered(ctrl as Control) as Boolean
    Dim bAllDataIsEntered As Boolean = True
    For Each ctrl In ctrl.Controls
        If TypeOf ctrl Is TextBox Then
            'using Trim disAllows spaces as data
            If ctrl.Text.Trim = String.Empty Then
                bAllDataIsEntered = False
                'this is new, may as well exit the loop now:
                Exit For
            End If
        ElseIf TypeOf ctrl Is CheckBox Then
            Dim chx As CheckBox = CType(ctrl, CheckBox)
            If Not chx.Checked Then
                bAllDataIsEntered = False
                'this is new, may as well exit the loop now:
                Exit For
            End If
        End If
    Next
    Return bAllDataIsEntered
End Function
Now just call the function with the name of the groupbox as the parameter.

For example: If AllDataEntered(groupbox1) then ...
 
Ok so this works great for checkboxes and textboxes. How do I modify it for comboboxes, I have this so far but am stick on what to do next

VB.NET:
[size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] combo [/size][size=2][color=#0000ff]As[/color][/size][size=2] ComboBox = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](ctrl, ComboBox)

[/size][size=2][color=#0000ff][size=2][color=#0000ff]If[/color][/size][size=2][color=#000000] [/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2][color=#000000] combo.Items.Count = 1 [/color][/size][size=2][color=#0000ff]Then[/color][/size][size=2][color=#000000] [/color][/size][size=2][color=#008000]'this line doesn't work, I don't know how to solve it

[/color][/size][/color][/size][size=2]bAllDataIsEntered = [/size][size=2][color=#0000ff]False

[/color][/size][size=2][/size][size=2][color=#008000]'this is new, may as well exit the loop now:

[/color][/size][size=2][/size][size=2][color=#0000ff]Exit[/color][/size][size=2] [/size][size=2][color=#0000ff]For

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size]
 
It really depends on how you have the comboBox setup.
If you have the comboBox's DropDownStyle set to DropDownList and the user can't type text in the comboBox, then you may want to enter an Item that says something like 'Select Something'. In this case either check to make sure the selected item isn't that item or that the text isn't 'Select Something'.
 
OK - ressurecting an old post...

If you have several text boxes, can you use the same procedure to deal with the textchanged event.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged


And, how do you enumerate through the text controls to:
1. check if ANY/ALL are empty; and
2. get the text in them

Ta - Andy
 
If you have several text boxes, can you use the same procedure to deal with the textchanged event.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

Not sure what you mean by the above bit....

But the textboxes are no problem...

'Create a HashTable to store the text from the textbox and use the key to identify what control the text is from.
VB.NET:
Dim TextBoxTextHashtable As New Hashtable
 
'Get The Next Control On Teh Form
[LEFT]Dim ctl As Control = Me.GetNextControl(Me, True)[/LEFT]
 
[LEFT]'Loop Through All The Controls
While ctl IsNot Nothing
'Determine If It Is A TextBox Of SomeKind
   If TypeOf ctl Is TextBoxBase Then
'Check To See If The Text Property Is Null
If Not String.IsNullOrEmpty(Ctl.Text)    
'If We Have Text Then Add The Name Of The Control To The Hashtable And The Text It Contains.
TextBoxHashTable.Add(Ctl.Name, Ctl.Text)
End If
'Get The Next Control
   ctl = Me.GetNextControl(ctl, True)
'Round We Go Again
End While[/LEFT]
 
Not sure what you mean by the above bit....

What I meant was - I would have to change the line
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

To TextBox2_TextChanged and TextBox3_TextChanged for each textbox
 
Yes you can...

VB.NET:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged ........etc.....etc..
 
Back
Top