Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

reference a control by string.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nate axtell - 27 Oct 2004 18:19 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.