I want to loop through and change all the label captions on my form.
Here is what I was trying:
VB Code:
For Each Label In Me Label.Caption = "New Caption" Next
I get a property not supported error. Any ideas?
Printable View
I want to loop through and change all the label captions on my form.
Here is what I was trying:
VB Code:
For Each Label In Me Label.Caption = "New Caption" Next
I get a property not supported error. Any ideas?
Did you name all the captions your trying to change label?
You need to have each label actually named "Label", meaning you'd have to have a control array. Each label would then be named, simply, "Label" with an index integer attached to it(i.e. Label(0), Label(1), Label(2), etc.).
Then it should work. This is a good way of doing things if you plan to make your app's labels customized(i.e. if you wanted to bold all your labels at once, un-bold them, italic, change the font, etc.). But it could get confusing if you later need to change 1 label's caption mid-code. Unless you have a good memory, you'd have to stop mid-code, click the label to find out what index number it is, and then go back to your code, etc.
It's a double-edged sword.
Yes they are named in this fashion LBL01, LBL02, LBL03 through LBL12. And when I message box label.name inside that loop it does pick up the labe's name.
you can loop through all the controls on your form, then if the typeof control = label you can run code, there are many examples of this if you search the forum
Correct and the code I posted is simpler than what I am actually doing. I will be changing the caption to something depending on the name of the label.Quote:
But it could get confusing if you later need to change 1 label's caption mid-code
westconn1:
My labels are inside a picture box and I the typeof control = Label keeps coming up false.
Is it because they are in the picture box?
That shouldn't be a problem, no. But then again, I could be wrong. I need VB on this computer :(Quote:
Originally Posted by ggettings
haha, ok Ill try moving one of the labels out of the picture box to prove that out.
Try this
VB Code:
Private Function ChangeCaption() Dim Ctl As Control Dim label As label For Each Ctl In Me Debug.Print Ctl.Container.Name If Ctl.Container.Name = "Picture1" Then If Ctl.Name = "label1" Then label.Caption = "New Caption" End If End If Next End Function
try like thisVB Code:
Dim c As Control For Each c In Controls If TypeOf c Is Label Then Debug.Print c.Name, c.Caption Next
VB Code:
For Each Label In Me If Label.Name = "LBL" & grid_line Then Label.Caption = grid_line & " (" & seq_sum & ")" Exit For End If Next
Well everything worked fine when I pulled one of the labels out of the picture box. I put that label back into the picure box and everything works?? I must have been doing something weird, you were right the picture box had no effect. Well its working thanks for all the help!
danasegarane: btw I didn't know about that .Container call, that could come in handy.