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,1 00,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.
userform3.label1.backcolor
because I want to pole these labels for more important information. Is there a for each command that I can set up.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.
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!
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!
ASKER
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