Control Collection problem, who to return control "index"

S

Steve

Hi,

I learned programming on VB5 and am now trying to get used to using control
collections instead of control arrays.

I have a set of 50 labels, labelled "Label1" "Label2" etc. I so far have the
code:

Dim intIndex As Integer
For intIndex = 0 To Me.Controls.Count - 1
If Left(Me.Controls(intIndex).Name, 1) = "L" Then
Me.Controls.(IntIndex).ForeColor = &H8000000D
Next intIndex

This selects all the labels and turns the text blue. I know the coding is a
bit sloppy but I'm okay with it. What I want to do though is find a way of
getting the labels to share the code as you could with control arrays, and
then get the program to be able to identify which label has been clicked on.
My goal is that any label is clicked on it will set the caption to the value
I have in a separately defined array.

To give a simply example:


myarray(1)="This is label1"
myarray(2)="This is label2"

Me.Controls(Index).Caption=myarray(index)

where index represents the collection index of the particular label that has
been clicked. I know that I could write out 50 separate pieces of code for
each label and just change the code manually but I was really hoping to avoid
this.

Any solutions would be great! Thanks
 
F

Fumei2 via OfficeKB.com

"My goal is that any label is clicked on "

The Click event for each control is separate.

Sub Label1_Click()

Sub Label2_Click()

Sub Label3_Click()

Sub Label4_Click()

Sub Label5_Click()


The problem is that there is no real controls collection, in that there is no
Index.

Me.Controls(x).Name returns the name of the control that was placed xth IN
ORDER. In other words, it is dynamic.

Say you have a TextBox, an OptionButton and a Checkbox, and they were placed
on the userform in that order. Then

Me.Controls(0) = TextBox1
Me.Controls(1) = OptionButton1
Me.Controls(2) = CheckBox1

Now suppose you delete the Textbox, go Oooops, I did not mean that, and make
a textbox back in the same place.

Me.Controls(0) = OptionButton1
Me.Controls(1) = CheckBox1
Me.Controls(2) = TextBox1

In both case you have three controls (and in the same place); in both cases
they have the same name - but they do NOT have the same varg.

It is Me.Controls(varg) - not Me.Controls(index)
 

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