I have a form that uses 28 checkboxes(checkBox1 ~ checkBox28), 28 labels(label1 ~ label28), and 28 textboxes (textBox1 ~ textBox28). These items need to be visible/invisible depending on the mode of the program (there are 5 modes). I could specify each item individually with visibility, but that requires a whole lotta code! Is there a way I can loop through 28 labels?

Any help is appreciated!

7 Years
Discussion Span
Last Post by bklynman01
For i As Integer = 1 To 28
                MsgBox("Number:" + i.ToString)

Just change what you need inside the For statement.

Edited by RenanLazarotto: Correct a little mistake


Here, I've made a code that might make things clear:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim CheckBoxNumber As Integer
        Dim NewCheckBoxTextValidator As Integer

        For i As Integer = 1 To 11
            CheckBoxNumber = i

            NewCheckBoxTextValidator = Me.Controls.IndexOfKey("CheckBox" & CheckBoxNumber.ToString)
            DirectCast(Me.Controls.Item(NewCheckBoxTextValidator), CheckBox).Text = "I am checkbox number " & i

    End Sub
End Class

Suppose i have 50 textboxes on my form and i want to : enable TextBox1 and Disable TextBox2.

So Do something like this

For Each ctrl As Control In Me.Controls

            If ctrl.Name = "TextBox1" Then
                ctrl.Enabled = False
            End If

            If ctrl.Name = "TextBox2" Then
                ctrl.Enabled = True
            End If


Above Loop will Loop through all the controls on the form including textbox, label, tab , combo etc etc..
u can enable or disable anyone with its name property

Edited by sandeepparekh9: n/a


This is what I ended up with. I solved this a long time ago, but forgot until I viewed my subscribed threads. Thank you everyone for the input.

            'go through all the controls in the tabpage and find the labels first.
            'insert the correct line item into the labels in numerical order
            For Each c As Control In Me.TabPage2.Controls
                If c.GetType.ToString = "System.Windows.Forms.Label" Then
                    'get the number at the end of the string. i.e.: txtOJTxx -> xx 
                    lblNo = c.Name()
                    lblNo = lblNo.Substring(6)
                    rowcnt = tblOJT.Rows.Count
                    If lblNo < (rowcnt + 1) Then
                        c.Visible = True
                        c.Text = tblOJT.Rows(lblNo - 1).Item("item")
                        'find the checkbox that corresponds to the label that was
                        'just filled in. if it is required, make it visible and enabled.
                        For Each cb As Control In Me.TabPage2.Controls
                            If cb.GetType.ToString = "System.Windows.Forms.CheckBox" Then
                                If cb.Name.Substring(6) = lblNo Then
                                    If tblOJT.Rows(lblNo - 1).Item("chkBoxReq") = "True" Then
                                        cb.Visible = True
                                        cb.Enabled = True
                                        cb.Visible = False
                                        cb.Enabled = False
                                    End If
                                End If
                            End If
                    End If
                End If
        Catch w As Exception
        End Try
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.