473,320 Members | 1,920 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,320 software developers and data experts.

Control collection

Ian
Hi

I can access a particular control on a form by it's index e.g. me.Controls.Item(0).Name

How would I access the control by name e.g. me.Controls.Item("Label1").Name. Is it possible?

Thanks

Ian

Nov 20 '05 #1
20 5437
Ian,
I Dont think this is directly possible, however, I might be proved wrong here. The problem is that the IndexOf and Contains methods both require a control to be passed to them.

However, if you wanted to do this, you could create a list, where the index number was the index in the controls collection, this way you could refer to it by name.

I would be interested to know if you do find a way other than that !

Regards - OHM


"Ian" <ih****@hotmail.com> wrote in message news:OF*************@tk2msftngp13.phx.gbl...
Hi

I can access a particular control on a form by it's index e.g. me.Controls.Item(0).Name

How would I access the control by name e.g. me.Controls.Item("Label1").Name. Is it possible?

Thanks

Ian

Nov 20 '05 #2
* "Ian" <ih****@hotmail.com> scripsit:
I can access a particular control on a form by it's index e.g. me.Controls.Item(0).Name

How would I access the control by name e.g. me.Controls.Item("Label1").Name.* Is it possible?


That's not possible, but you can add all controls to a 'Hashtable'
((name = key, value = reference) pairs) and then access the controls
through the hashtable.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
"Ian" <ih****@hotmail.com> schrieb

I can access a particular control on a form by it's index e.g.
me.Controls.Item(0).Name

How would I access the control by name e.g.
me.Controls.Item("Label1").Name. Is it possible?

Me.Label1.Text = "it's me"
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
Armin,
He wants to dereference the control by name. See the posts
Herfied and Myself have made.

Regards - OHM
Armin Zingler wrote:
"Ian" <ih****@hotmail.com> schrieb

I can access a particular control on a form by it's index e.g.
me.Controls.Item(0).Name

How would I access the control by name e.g.
me.Controls.Item("Label1").Name. Is it possible?

Me.Label1.Text = "it's me"

Nov 20 '05 #5
* "One Handed Man" <Bo****@Duck.net> scripsit:
He wants to dereference the control by name. See the posts
Herfied and Myself have made.


Armin is dereferencing the contol by the name of an instance variable
pointing to it.

;-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Yes, but not using the controls collection. However, having said that this
could have been of use to the OP.

Herfied, Thats not what came to your mind or mine when reading the OP was it
?

See the original post
me.Controls.Item("Label1").Name. Is it possible?

Regards - OHM
Herfried K. Wagner [MVP] wrote: * "One Handed Man" <Bo****@Duck.net> scripsit:
He wants to dereference the control by name. See the posts
Herfied and Myself have made.


Armin is dereferencing the contol by the name of an instance variable
pointing to it.

;-)

Nov 20 '05 #7
* "One Handed Man" <Bo****@Duck.net> scripsit:
Yes, but not using the controls collection. However, having said that this
could have been of use to the OP.

Herfied, Thats not what came to your mind or mine when reading the OP was it
?


ACK -- it didn't come in my mind too.

;-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #8
Ian
Basically, what I want to do is this:

For i = 1 to 5
Dim x as String = "Label" & i

me.Controls.Item(x).Text = "Hello"

Next i

Ian
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@hercules.btinternet.com...
Yes, but not using the controls collection. However, having said that this could have been of use to the OP.

Herfied, Thats not what came to your mind or mine when reading the OP was it ?

See the original post
me.Controls.Item("Label1").Name. Is it possible?

Regards - OHM
Herfried K. Wagner [MVP] wrote:
* "One Handed Man" <Bo****@Duck.net> scripsit:
He wants to dereference the control by name. See the posts
Herfied and Myself have made.


Armin is dereferencing the contol by the name of an instance variable
pointing to it.

;-)


Nov 20 '05 #9
* "Ian" <ih****@hotmail.com> scripsit:
Basically, what I want to do is this:

For i = 1 to 5
Dim x as String = "Label" & i

me.Controls.Item(x).Text = "Hello"

Next i


Mhm... Something like control arrays.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET
<http://msdn.microsoft.com/library/?url=/library/en-us/dv_vstechart/html/vbtchCreatingControlArraysInVisualBasicNETVisualCN ET.asp>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #10
"One Handed Man" <Bo****@Duck.net> schrieb
Armin,
He wants to dereference the control by name. See the
posts
Herfied and Myself have made.


I think Label1 *is* the name.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #11
Ian
yes exactly, but control arrays are not supported in .Net

Ian

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bq*************@ID-208219.news.uni-berlin.de...
* "Ian" <ih****@hotmail.com> scripsit:
Basically, what I want to do is this:

For i = 1 to 5
Dim x as String = "Label" & i

me.Controls.Item(x).Text = "Hello"

Next i
Mhm... Something like control arrays.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET

<http://msdn.microsoft.com/library/?u...chart/html/vbt
chCreatingControlArraysInVisualBasicNETVisualCNET. asp>
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #12
Ian
The example shows how to create control arrays at runtime. I don't need
that. I want to iterate through controls 'by name' through the controls
collection.

If it can't be done, then somebody say so.

Ian

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bq*************@ID-208219.news.uni-berlin.de...
* "Ian" <ih****@hotmail.com> scripsit:
Basically, what I want to do is this:

For i = 1 to 5
Dim x as String = "Label" & i

me.Controls.Item(x).Text = "Hello"

Next i
Mhm... Something like control arrays.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET

<http://msdn.microsoft.com/library/?u...chart/html/vbt
chCreatingControlArraysInVisualBasicNETVisualCNET. asp>
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #13
* "Ian" <ih****@hotmail.com> scripsit:
yes exactly, but control arrays are not supported in .Net


They are...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #14
* "Armin Zingler" <az*******@freenet.de> scripsit:
He wants to dereference the control by name. See the
posts Herfied and Myself have made.


I think Label1 *is* the name.


It's the name of the variable the designer generates -- and it's the
name.

;-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #15
* "Ian" <ih****@hotmail.com> scripsit:
The example shows how to create control arrays at runtime. I don't need
that. I want to iterate through controls 'by name' through the controls
collection.


With the 'Hashtable':

You can give every picturebox a name by setting its 'Name' property.
Then you can add them to a hashtable:

\\\
Private m_ht As New Hashtable()
..
..
..

' Add a control (use name as key).
m_ht.Add(DynamicPictureBox.Name, DynamicPictureBox)
..
..
..

' Get a control.
Dim p As PictureBox = DirectCast(m_ht.Item("PictureBox1"), PictureBox)

..
..
..

' Remove a control.
m_ht.Remove("PictureBox1")
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #16
Cor
Why do you start a complete long seperate thread,
This I have answered the OP at 9 o'clock

----------------------------------------------------------------------------
-
Hi Ian,

For normal purpose (not dynamicly making controls) you don't need the
control array. You can loop through controls.

Keep in mind that a control is always a child of his parent.

I take your example and make it the same in a for each loop.
I know this sounds a little bit strange and you can think on a control
array,
on the other hand here you have an example how to do that without it when
your labels (childs) are direct on the form (parent)

This example is very quick and dirty written so watch typos and small errors
\\\
dim ctr as control
for each ctr in me.controls
if typof ctr is label then
if ctr.name.substring(0,5) = "label" then
ctr.text = array(ctr.name.substring(5,1))
end if
end if
next
///

Of course when you have more labels with the name label, you should test
first if the labelname was between label1 and label5
For i = 0 to 4
Label(i).text = array(i)
Next i


Nov 20 '05 #17
* "Cor" <no*@non.com> scripsit:
Why do you start a complete long seperate thread,
This I have answered the OP at 9 o'clock


Where? For some reason, I don't see your post. Are you sure it was
sent properly?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #18
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Cor" <no*@non.com> scripsit:
Why do you start a complete long seperate thread,
This I have answered the OP at 9 o'clock


Where? For some reason, I don't see your post. Are you sure it
was sent properly?

I also can't see it.
--
Armin

Nov 20 '05 #19
Cor
Hi Armin,
Where? For some reason, I don't see your post. Are you sure it
was sent properly?

I also can't see it.


Will you check it, because this is weird I see it on the Microsoft server

The message was

Populating labels from an array

Thanks

Cor
Nov 20 '05 #20
"Cor" <no*@non.com> schrieb
Where? For some reason, I don't see your post. Are you sure
it was sent properly?

I also can't see it.


Will you check it, because this is weird I see it on the Microsoft
server

The message was

Populating labels from an array


Ahh.. ok... I thought the message is in _this_ thread. Yes, I can see the
other one.
--
Armin

Nov 20 '05 #21

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

Similar topics

4
by: Mark | last post by:
How do you reference the caption of the label attached to a control on a form by first referencing the form? For example, textbox named Addr1 has an attached label with the caption "Address1"....
1
by: Rahim | last post by:
i want to change all the label control style Properties, server control properties at runtime how should i call all the label at runtime, which is present at webform, any collections???? i...
0
by: Tom | last post by:
I am developing a page that will contain multiple instances of a Composite Custom Control that i have developed. The problem is that the user will determine at run time how many of the control...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
3
by: Simon Harvey | last post by:
Hi All. In one of my user controls I add a textbox to a placeholder sitting on the user control. txtUsername = new TextBox(); txtUsername.ID = "txtUsername";...
1
by: Dot net work | last post by:
Hello. I have an interesting data binding scenario: I have a repeater control. It repeats a typical custom web user control. I also have a collection object, and each collection element...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
0
by: Rene Aichinger | last post by:
Hi, I've created a server control (it inherits from WebControl) wich has a property of genertic collection type. The type of the generic collection is NavigationItem, a little class I've...
5
by: paul.hester | last post by:
Hi all, I have a custom control with an overridden Render method. Inside this method I'm rendering each control in its collection using their RenderControl method. However, I'm running into a...
14
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I give the DataSourceId and in the custom-control...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.