Hello all,
I have a form with 24 textboxes incremented by a letter.
example: txtDimVala1.text, txtDimValb1.text, txtDimValc1.text, etc.

I created a code to check each textbox, but there must me a more efficient way to loop it. I have tried a few loops but I cant get them to work looping through the different textbox values.

Thanks for any help in advance.

below is the code i already have

Private Sub CheckDims()

        If (IsNumeric(txtDimVala1.Text)) Then
            Dim num As Decimal = txtDimVala1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimVala1.BackColor = Color.LightSalmon
            Else
                txtDimVala1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValb1.Text)) Then
            Dim num As Decimal = txtDimValb1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValb1.BackColor = Color.LightSalmon
            Else
                txtDimValb1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValc1.Text)) Then
            Dim num As Decimal = txtDimValc1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValc1.BackColor = Color.LightSalmon
            Else
                txtDimValc1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimVald1.Text)) Then
            Dim num As Decimal = txtDimVald1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimVald1.BackColor = Color.LightSalmon
            Else
                txtDimVald1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimVale1.Text)) Then
            Dim num As Decimal = txtDimVale1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimVale1.BackColor = Color.LightSalmon
            Else
                txtDimVale1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValf1.Text)) Then
            Dim num As Decimal = txtDimValf1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValf1.BackColor = Color.LightSalmon
            Else
                txtDimValf1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValg1.Text)) Then
            Dim num As Decimal = txtDimValg1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValg1.BackColor = Color.LightSalmon
            Else
                txtDimValg1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValh1.Text)) Then
            Dim num As Decimal = txtDimValh1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValh1.BackColor = Color.LightSalmon
            Else
                txtDimValh1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimVali1.Text)) Then
            Dim num As Decimal = txtDimVali1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimVali1.BackColor = Color.LightSalmon
            Else
                txtDimVali1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValj1.Text)) Then
            Dim num As Decimal = txtDimValj1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValj1.BackColor = Color.LightSalmon
            Else
                txtDimValj1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValk1.Text)) Then
            Dim num As Decimal = txtDimValk1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValk1.BackColor = Color.LightSalmon
            Else
                txtDimValk1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimVall1.Text)) Then
            Dim num As Decimal = txtDimVall1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimVall1.BackColor = Color.LightSalmon
            Else
                txtDimVall1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValm1.Text)) Then
            Dim num As Decimal = txtDimValm1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValm1.BackColor = Color.LightSalmon
            Else
                txtDimValm1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValn1.Text)) Then
            Dim num As Decimal = txtDimValn1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValn1.BackColor = Color.LightSalmon
            Else
                txtDimValn1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValo1.Text)) Then
            Dim num As Decimal = txtDimValo1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValo1.BackColor = Color.LightSalmon
            Else
                txtDimValo1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValp1.Text)) Then
            Dim num As Decimal = txtDimValp1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValp1.BackColor = Color.LightSalmon
            Else
                txtDimValp1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValq1.Text)) Then
            Dim num As Decimal = txtDimValq1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValq1.BackColor = Color.LightSalmon
            Else
                txtDimValq1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValr1.Text)) Then
            Dim num As Decimal = txtDimValr1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValr1.BackColor = Color.LightSalmon
            Else
                txtDimValr1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimVals1.Text)) Then
            Dim num As Decimal = txtDimVals1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimVals1.BackColor = Color.LightSalmon
            Else
                txtDimVals1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValt1.Text)) Then
            Dim num As Decimal = txtDimValt1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValt1.BackColor = Color.LightSalmon
            Else
                txtDimValt1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValu1.Text)) Then
            Dim num As Decimal = txtDimValu1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValu1.BackColor = Color.LightSalmon
            Else
                txtDimValu1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValv1.Text)) Then
            Dim num As Decimal = txtDimValv1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValv1.BackColor = Color.LightSalmon
            Else
                txtDimValv1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValw1.Text)) Then
            Dim num As Decimal = txtDimValw1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValw1.BackColor = Color.LightSalmon
            Else
                txtDimValw1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValx1.Text)) Then
            Dim num As Decimal = txtDimValx1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValx1.BackColor = Color.LightSalmon
            Else
                txtDimValx1.BackColor = Color.White
            End If
        End If

        If (IsNumeric(txtDimValy1.Text)) Then
            Dim num As Decimal = txtDimValy1.Text
            Dim num2 As Decimal = txtDimL1.Text
            Dim num3 As Decimal = txtDimU1.Text

            If num < num2 Or num > num3 Then
                txtDimValy1.BackColor = Color.LightSalmon
            Else
                txtDimValy1.BackColor = Color.White
            End If
        End If

    End Sub

Recommended Answers

All 9 Replies

I haven't really used this, but if I remember correctly you should use something like:

for each ctrl as Control in Me.Controls 
if typeof ctrl is textbox then 
'your code here 
end if 
next

I'm pretty sure you can also check the name of the control, I'll look it up and get back to you.

Edit: Found a sample here: http://www.java2s.com/Code/VB/GUI/GetLabelsfromallControlsonaForm.htm

If (ctl.Name.StartsWith("lbl")) AndAlso (TypeOf ctl Is Label) Then

Hey Adam,

I tried using your example but was not succesful, this is what my code looks like but i couldnt get it to return a value.

Private Sub check()
        For Each ctrl As Control In Me.Controls
            If (ctrl.Name.StartsWith("txtDimVal")) AndAlso (TypeOf ctrl Is TextBox) Then
                txtMo = DirectCast(ctrl, TextBox)
                MsgBox(txtMo)
            End If
        Next ctrl
    End Sub

What is the issue or problem with above code? i mean whats not hapening?

EDIT: have you declared txtmemo as textbox?
and what is MsgBox(txtMo)? txtmemo.name? txtmemo.text?

well i wanted it to loop through the textboxes, and give me the value. I was just trying to use Adams example.

My main problem is I need to loop through various textboxes to get the values. While increment a letter in the name of the textbox.
example:
txtDimVala1, txtDimValb1, txtDimValc1.
I need the take the values of each textbox and compare it to another value.

Dim txtMo As TextBox
        For Each ctrl As Control In Me.Controls
            If (ctrl.Name.StartsWith("txtDimVal")) AndAlso (TypeOf ctrl Is TextBox) Then
                txtMo = DirectCast(ctrl, TextBox)
                MsgBox(txtMo.Text)
            End If
        Next ctrl

you want to compare the text?

Im sorry this thread has gotten a bit confusing. The code below is exactly what i want to do but i get a runtime error of "Object reference not set to an instance of an object." for the line "If Me.Controls("txtDimVala" & index).Text = "" Then".

Private Sub sample()

        For index As Integer = 1 To 25
            If Me.Controls("txtDimVala" & index).Text = "" Then

            Else
                If (IsNumeric(Me.Controls("txtDimVala" & index).Text)) Then
                    Dim num As Single = Me.Controls("txtDimVala" & index).Text
                    Dim num2 As Single = Me.Controls("txtDimL" & index).Text
                    Dim num3 As Single = Me.Controls("txtDimU" & index).Text

                    If num < num2 Or num > num3 Then
                        Me.Controls("txtDimVala" & index).BackColor = Color.LightSalmon
                    Else
                        Me.Controls("txtDimVala" & index).BackColor = Color.White
                    End If
                Else
                    Me.Controls("txtDimVala" & index).BackColor = Color.White
                End If
            End If
        Next
    End Sub

Are you looking for specific textbox to get the value of num , num 2,num 3?

yes i have a specific textboxes that pull the value for num, num2 and num3?
num would be txtDimVala1.Text
num2 would be txtDimL1.Text
num3 would be txtDimU1.Text

then for the next textboxes it should change to
num would be txtDimVala2.Text
num2 would be txtDimL2.Text
num3 would be txtDimU2.Text

Thanks for your help everyone, but i finally figured it out. I using the me.control.item but i had the text box in a tab control and panel so i had to add those as control items also.

The working code looks like this:

For index As Integer = 1 To 25
            If Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimVala" & index).Text() = "" Then              
            Else
                If (IsNumeric(Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimVala" & index).Text())) Then
                    Dim num As Single = Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimVala" & index).Text()
                    Dim num2 As Single = Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimL" & index).Text()
                    Dim num3 As Single = Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimU" & index).Text()

                    If num < num2 Or num > num3 Then
                        Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimVala" & index).BackColor() = Color.LightSalmon
                    Else
                        Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimVala" & index).BackColor() = Color.White
                    End If
                Else
                    Me.Controls.Item("TabControl1").Controls.Item("TabPage2").Controls.Item("TableLayoutPanel1").Controls.Item("txtDimVala" & index).BackColor() = Color.White
                End If
            End If
        Next index
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.