Traverse form buttons

  • Thread starter Thread starter Sebastian Santacroce
  • Start date Start date
S

Sebastian Santacroce

Is there some easy programmable way of traversing all
button in a form and set their enabled state to false?

Something like

for each button x in this.form
x.enabled=false
end for

Thank you

PS Why doesn't this newsgroup email me anymore when I get
a response to my post?

Sebastian
 
Here's a way to do it...

Dim castControl As Windows.Forms.Control
Dim objButton As New Button

For Each castControl In yourFormHere.Controls
If castControl.GetType.Equals(objButton.GetType) Then
castControl.Enabled = False
End If
Next

Dunno about the emails thing.
 
Hi Sebastian,

The following will do the trick for all Buttons on the Form and all those
contained within Panels and/or other Controls. It works by going through each
Control's collection of child Controls. You can start it with a Form (which is
a Control itself) or with any Control on the Form.

Call it using:
EnableAllButtons (Me, TrueOrFalse)

Sub EnableAllButtons (oControl As Control, EnabledState As Boolean)
Dim oChildControl As Control
For Each oChildControl in oControl.Controls
If TypeOf (oChildControl) Is Button Then
oChildControl.Enabled = EnabledState
End If
EnableAllButtons (oChildControl, EnabledState)
Next
End Sub

Naturally this can be adapted to do anything with all the Controls of any
particular type.

Regards,
Fergus
 
Back
Top