I Want to set property for many same Tools?

Nader

Well-known member
Joined
Oct 9, 2008
Messages
55
Programming Experience
Beginner
I have many RadioButton on the Form and I want to make Enable for them, So this will need many like this code RadioButton.Enabled= False.
I tried this code to set the property for all Tools but doens't susccess with me.

VB.NET:
[LEFT][FONT=Courier New][COLOR=blue][COLOR=blue][FONT=Courier New]For[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Each[/COLOR] RadB [COLOR=blue]As[/COLOR] Control [COLOR=blue]In[/COLOR] Controls[/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=blue][FONT=Courier New][COLOR=blue]If[/COLOR] [COLOR=blue]TypeOf[/COLOR] RadB [COLOR=blue]Is[/COLOR] RadioButton [COLOR=blue]Then[/COLOR] RadB.Enabled = [COLOR=blue]False[/COLOR][/FONT][/COLOR][/FONT][/LEFT]
[FONT=Courier New][COLOR=blue][COLOR=blue][FONT=Courier New]Next[/FONT][/COLOR]
[/COLOR][/FONT]
 
You need to cast it:
VB.NET:
For Each RadB As Control In Me.Controls
    If TypeOf RadB Is RadioButton Then DirectCast(RadB, RadioButton).Enabled = False
Next
 
Enabled is an inherited property of Control class, so you don't need to cast a variable of type Control to access this property. Perhaps what is failing you is that you have the radio buttons in a different container? Perhaps you need to loop Panel1.Controls instead of Me.Controls? To iterate controls in any container look into using Form.GetNextControl method.
 
Back
Top