Control collection

I

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
 
O

One Handed Man

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




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
 
H

Herfried K. Wagner [MVP]

* "Ian said:
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.
 
A

Armin Zingler

Ian said:
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"
 
O

One Handed Man

Armin,
He wants to dereference the control by name. See the posts
Herfied and Myself have made.

Regards - OHM
 
H

Herfried K. Wagner [MVP]

* "One Handed Man said:
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.

;-)
 
O

One Handed Man

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
 
H

Herfried K. Wagner [MVP]

* "One Handed Man said:
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.

;-)
 
I

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
 
I

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
 
H

Herfried K. Wagner [MVP]

* "Ian said:
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")
///
 
C

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
 
H

Herfried K. Wagner [MVP]

* "Cor said:
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?
 
C

Cor

Hi Armin,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top