DirectCast name of control

mevets

Member
Joined
Jun 22, 2006
Messages
17
Location
Northern, VA
Programming Experience
Beginner
I am trying to set the backcolor to a control. The user provides the name of the control in a TextBox. How is this done?

Unsuccessful with:
VB.NET:
        Dim nameofctrl As Object = Textbox1.text
        DirectCast(nameofctrl, Control).BackColor = Color.Green
 
May be try this:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] nameofctrl() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls.Find(TextBox1.Text, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aCtrl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] aCtrl [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] nameofctrl
[/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](aCtrl, Control).BackColor = Color.Green
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
 
Back
Top