I'm trying to iterate through the controls on a form and set the tabIndex to 0 for all labels. I copped the following code from google but it doesn't work for me and I'm pretty certain it's because all of my controls are on a panel and not on the form. Does anyone know how I can modify this code to work if the controls are on a panel. I've tried lots of things with no success.

Private Sub setControls()
    Dim cControl as control
    For each cControl in me.controls
        If (Typeof cControl is Label) then 
            cControl.tabIndex = 0
        end if
    next cControl
end sub

Recommended Answers

All 5 Replies

Change Me to the name of the panel. Alternatively you can use For Each cControl As Control In Panel1.Controls.OfType(Of Label) to iterate through just the labels instead of every control.

A control can be a container for other controls, so such an algorithm should be recursive:

Private Sub SetControls(ByVal Control As parentCtrl)
    If TypeOf parentCtrl Is Label Then
        ' Set the label properties you want
    End If

    For Each ctrl As Control In parentCtrl.Controls
        SetControls(ctrl)
    Next
End Sub

Perfect!! Thanks so much tinstaffl and deceptikon.

You're very welcome. Please rememeber to mark this solved. Thanks.

I thought I did. I clicked the Mark Question Solved button. Let's see if I get it right this time. Thanks Tinstaafl

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.