Link to home
Start Free TrialLog in
Avatar of jcherry_99
jcherry_99

asked on

Changing the color of a label on a form when there are many labels

I am working with a form that has 65 small labels on it. I want to change the color of them to some color that is not in the backcolor pulldown. Putting in 65 lines of code all looking like

userform3.label1.backcolor=rgb(240,100,0) is a night mare

because I want to pole these labels for more important information. Is there a for each command that I can set up.
SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jcherry_99
jcherry_99

ASKER

I am perfectly happy with the second answer. I've tried it and it works. But before signing off on the question, I'd like to ask something on the first (I'll increase the number of points).

1. what are you clicking to get to the subroutine you've typed.
2. How do you make the array? I'm not sure I know how to do that.


Thank you both so much for your quick response.

Jerome
Hi Jerome
The best place to put the code is somewhere which gets run early in your program run. That is probably in your initial Form_Load event. You won't have to click anything.
Graham
Sorry, that means
Sub Form_Load '(in Userform3)
For each ctl in Me.Controls
    if left$(ctl.name,5) = "Label" then
           ctl.BackColor = RGB(240, 100, 0)
   endif
next ctl
end sub

To create an array you would add a label and set the index property to 0. Then you would add more labels with the same name and they would each have a different index.They can then be referred to by index #.

Label1(0).Caption = "Label 1 Text"
Label1(1).Caption = "Label 2 Text"
Label1(2).Caption = "Label 3 Text"

The advantage to this is that a VB form can only contain 255 controls.By adding them into a control array,all 65 Labels will count as 1.
Hi,
Although you are satisfied with GrahamSkan comments here I am with one more way to do that-

Private Sub Form_Load()
    Dim obj As Object
    For Each obj In Me.Controls
        If TypeName(obj) = "Label" Then
            obj.BackColor = RGB(240, 100, 0)
        End If
    Next
End Sub

Cheers!