Control collection

  • Thread starter Thread starter Ian
  • Start date Start date
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
 
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
 
* "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.
 
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"
 
Armin,
He wants to dereference the control by name. See the posts
Herfied and Myself have made.

Regards - OHM
 
* "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.

;-)
 
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
 
* "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.

;-)
 
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
 
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
 
* "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")
///
 
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
 
* "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?
 
Back
Top