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

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 2 Years Ago 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
        SetControls(ctrl)
    Next
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.