|
-
Oct 26th, 2006, 07:38 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Change all label captoins on a form
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?
-
Oct 26th, 2006, 07:42 AM
#2
Member
Re: Change all label captoins on a form
Did you name all the captions your trying to change label?
If life was something money could buy, the poor would not live and the rich would not die.
-
Oct 26th, 2006, 07:47 AM
#3
Hyperactive Member
Re: Change all label captoins on a form
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.
-
Oct 26th, 2006, 07:51 AM
#4
Thread Starter
Hyperactive Member
Re: Change all label captoins on a form
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.
-
Oct 26th, 2006, 07:51 AM
#5
Re: Change all label captoins on a form
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Oct 26th, 2006, 07:59 AM
#6
Thread Starter
Hyperactive Member
Re: Change all label captoins on a form
But it could get confusing if you later need to change 1 label's caption mid-code
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.
-
Oct 26th, 2006, 08:03 AM
#7
Thread Starter
Hyperactive Member
Re: Change all label captoins on a form
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?
-
Oct 26th, 2006, 08:07 AM
#8
Hyperactive Member
Re: Change all label captoins on a form
 Originally Posted by ggettings
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
-
Oct 26th, 2006, 08:09 AM
#9
Thread Starter
Hyperactive Member
Re: Change all label captoins on a form
haha, ok Ill try moving one of the labels out of the picture box to prove that out.
-
Oct 26th, 2006, 08:10 AM
#10
Re: Change all label captoins on a form
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
-
Oct 26th, 2006, 08:17 AM
#11
Re: Change all label captoins on a form
try like this
VB Code:
Dim c As Control
For Each c In Controls
If TypeOf c Is Label Then Debug.Print c.Name, c.Caption
Next
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Oct 26th, 2006, 08:19 AM
#12
Thread Starter
Hyperactive Member
Re: Change all label captoins on a form
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!
-
Oct 26th, 2006, 08:22 AM
#13
Thread Starter
Hyperactive Member
Re: Change all label captoins on a form
danasegarane: btw I didn't know about that .Container call, that could come in handy.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|