1.11M Members

iterating through, and assigning properties, to controls on a form


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.


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

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

Question Answered as of 6 Months Ago by tinstaafl and deceptikon
This question has already been solved: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: