Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control.
Dim ctlCount As Integer = 0
While Not ctl Is Nothing
ctlCount += 1
If TypeOf ctl Is TextBoxBase Then
CType(ctl, TextBoxBase).ReadOnly = True
End If
ctl = Me.GetNextControl(ctl, True)
End While
MessageBox.Show(ctlCount.ToString() & " controls found.")
Dim ctl As Control = Me.TopLevelControl.GetNextControl(Me.TopLevelControl, True) 'Get the first control.
Dim ctlCount As Integer = 0
While Not ctl Is Nothing
ctlCount += 1
If TypeOf ctl Is TextBoxBase Then
CType(ctl, TextBoxBase).ReadOnly = True
End If
ctl = Me.TopLevelControl.GetNextControl(ctl, True)
End While
If you want to differentiate between a few different types of controls here's a trick a learned recently:ImDaFrEaK said:Good stuff JMCILHINNEY.... I didn't even know that TypOf and TopLeveControl were methods you could use. This saves me a headache b/c I tried hard to find a way to differentiate between different controls. Also, now I know how to search every control in the form jus as so and add a recursive call to get all controls. Or at least I think I can get that to work.
For Each ctl As Control In Me.Controls
Select Case True
Case TypeOf ctl Is TextBox
Dim myTextBox As TextBox = DirectCast(ctl, TextBox)
Case TypeOf ctl Is Label
Dim myLabel As Label = DirectCast(ctl, Label)
End Select
Next