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
4 Years
Discussion Span
Last Post by kentuckyjoe

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.

Edited by tinstaafl


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
End Sub

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

This question has already been answered. 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.