Access Control from name stored in String

ShootingStar

Member
Joined
Jul 8, 2005
Messages
5
Programming Experience
3-5
I have the name of a control in a string. I want to access the properties etc of that control. If I try to use me.controls() the index of the control is required. How do I access the control based on the name. OR how do I get the index of a control from its name?
 
You can use Reflection, which you can look up in the help documentation, or you can itertate over all controls and test the Name property until you find the one you want.
 
Ok, i hope this is enough to get a sense ...

say you want to find control Button1 ... in that case you can do something like this

VB.NET:
Dim[/color][/size][size=2] strControl [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Button1"
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] xControl [/size][size=2][color=#0000ff]As[/color][/size][size=2] Control
 
[/size][size=2][color=#0000ff]For[/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] xControl [/size][size=2][color=#0000ff]In[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] xControl.Name = strControl [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]xControl.Text = "found" 'the text property of certain button would be 'found'
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]Next

Cheers ;)
 
Back
Top