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!

For i As Integer = 1 To 28
                MsgBox("Number:" + i.ToString)
            Next

Just change what you need inside the For statement.

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

        Next
    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

        Next

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

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.

Try
            '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
                                    Else
                                        cb.Visible = False
                                        cb.Enabled = False
                                    End If
                                End If
                            End If
                        Next
                    End If
                End If
            Next
        Catch w As Exception
            MsgBox(w.ToString)
        End Try
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.