Results 1 to 13 of 13

Thread: [RESOLVED] Change all label captoins on a form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Resolved [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:
    1. For Each Label In Me
    2.    Label.Caption = "New Caption"
    3. Next

    I get a property not supported error. Any ideas?

  2. #2
    Member
    Join Date
    Oct 2006
    Location
    England
    Posts
    49

    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.

  3. #3
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    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.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    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.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    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?

  8. #8
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Change all label captoins on a form

    Quote 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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    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.

  10. #10
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Change all label captoins on a form

    Try this
    VB Code:
    1. Private Function ChangeCaption()
    2.     Dim Ctl As Control
    3.     Dim label As label
    4.     For Each Ctl In Me
    5.         Debug.Print Ctl.Container.Name
    6.         If Ctl.Container.Name = "Picture1" Then
    7.             If Ctl.Name = "label1" Then
    8.                 label.Caption = "New Caption"
    9.             End If
    10.     End If
    11.     Next
    12.  
    13. End Function

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change all label captoins on a form

    try like this
    VB Code:
    1. Dim c As Control
    2. For Each c In Controls
    3.  If TypeOf c Is Label Then Debug.Print c.Name, c.Caption
    4. 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

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Re: Change all label captoins on a form

    VB Code:
    1. For Each Label In Me
    2.      If Label.Name = "LBL" & grid_line Then
    3.                Label.Caption = grid_line & " (" & seq_sum & ")"
    4.      Exit For
    5.      End If
    6. 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!

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    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
  •  



Click Here to Expand Forum to Full Width