473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

reference a control by string.

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
Nov 21 '05 #1
4 1984
"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>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
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.
"nate axtell" wrote:
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

Nov 21 '05 #3
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
"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>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4
I went with the Hashtable method, thanks.
nate

"Charlie" <Ch*****@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
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.
"nate axtell" wrote:
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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob Cannistraci | last post by:
A three-tier user authentication system was running without a problem for almost a year and now is suddenly dysfunctional. We don't know of any changes to any of the servers. It's quite maddening....
6
by: michaelkatsilis | last post by:
Hi, Are there any issues with returning a const reference value for public "get" accessor methods? eg. const string & getVal(); // or const MyString & getVal();
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
2
by: Zippy | last post by:
Some months ago, we requested help from this newsgroup on how to replace the library reference of a database with another library reference, prior to creating an MDE. I got the following answer...
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
3
by: Dave | last post by:
Greetings, I have a user control in my web form. I cannot reference it in code. My HTML File has the header: <%@ Register TagPrefix="PLB" Tagname="PIPSidebar" src="ProjectListSidebar.ascx"...
3
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server...
3
by: SAL | last post by:
I am getting the following ERROR in my WebApp on line 30: Server Error in '/TestWebApp' Application. -------------------------------------------------------------------------------- Object...
1
by: Nathan Sokalski | last post by:
I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this UserControl looks like the following: Partial Public...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.