I'm looking for a way to refernce a control by a string that represents the
name of the control. I dynamically create some textboxes, labels, and
comboboxes each name by a loop iteration index. (E.G.: "combo" & i) These
are added to the controls of a panel. How can I at a later time reference
these items in a panel by using "combo0", "combo1", etc... using a string
variable? An example would be the removal of one of the items from the
control. So far all I can find is:
Me.myPanel.Controls.Remove(myControl)
or
Me.myPanel..Controls.IndexOf(myControl)
Any suggestions?
thanks
nate
Herfried K. Wagner [MVP] - 27 Oct 2004 18:27 GMT
"nate axtell" <naxtell at progeny dot net> schrieb:
> I'm looking for a way to refernce a control by a string
> that represents the name of the control.
Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

Signature
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/
nate axtell - 27 Oct 2004 18:47 GMT
I was also hoping not to have to loop through all of the controls, so I
think I will use the HashTable or Array method that it suggests.
Thanks for the link.
Does anyone have another method?
nate
> "nate axtell" <naxtell at progeny dot net> schrieb:
>> I'm looking for a way to refernce a control by a string
>> that represents the name of the control.
>
> Accessing controls by their names or indices
> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
Charlie - 27 Oct 2004 18:47 GMT
Create a Hashtable
When you create the control, add it to the hashtable, using the name you
create as key.
When you need access to the control, you should declare a variable as the
control type and use directcast to cast it correctly.
When you create controls:
dim HT as new Hashtable
(in loop)
Dim Txt as new Textbox
...
HT.add("T1", Txt)
Dim Cbo ...
HT.add("C1", Cbo)
When you need to use a control:
Select Case HT("T1").gettype.name.tolower
Case "textbox"
Dim Txt as Textbox = Directcast(HT("T1"), Textbox)
Case "combobox"
...
End Select
You can loop through a Hashtable using For Each Loop.
> I'm looking for a way to refernce a control by a string that represents the
> name of the control. I dynamically create some textboxes, labels, and
[quoted text clipped - 10 lines]
> thanks
> nate
nate axtell - 27 Oct 2004 20:45 GMT
I went with the Hashtable method, thanks.
nate
> Create a Hashtable
>
[quoted text clipped - 41 lines]
>> thanks
>> nate