Modify many label at once

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • municipal
    New Member
    • Jan 2007
    • 4

    Modify many label at once

    Hi,

    I have a few dozen of labels on the stage. Knowing that, I've named them so that they all contain the word label, followed by a number: Label1 ~ Label28

    Is there anyway to change each Labels' caption (am using VB6) through a loop with x the increasing integer:
    "Label" & x.caption = Array x...

    hope this makes sense and there's actually a way around it.

    thanks

    Muni
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Yes, you can do that. But you should be using a "control array". What you're trying to do now is a crude way of simulating an array.

    Just give the labels the same name, with different index settings. The quickest way to do so is to create a label, name it, then copy it and paste back to the form. VB will ask whether you want to make it an array - say "yes".

    You should have some idea of how powerful arrays are (mostly due to the possibility of loop processing) - well, an array of controls is just as good, if not better.

    Interestingly, VB.Net doesn't support control arrays, so people end up having to simulate them, more or less the way you are.

    Comment

    • sukeshchand
      New Member
      • Jan 2007
      • 88

      #3
      Another method is to write a for each loop and check the name of the label and rename its caption

      like
      Code:
      dim mc as Variant
      For each mc in me.controls
           if typeof mc is  Label then 
                   'Change the caption here
           end if
      next

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Variants - ugh! I'd recommend using Dim mc As Control.

        In any case, while this is certainly a useful technique in some circumstances, a control array offers many advantages.

        Comment

        • municipal
          New Member
          • Jan 2007
          • 4

          #5
          thanks a lot for the replies killer42 and sukeshchand.

          For this I decided to go with control arrays, but that loop might come in handy some time in the future.

          thanks again.
          muni

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by municipal
            thanks a lot for the replies killer42 and sukeshchand.

            For this I decided to go with control arrays, but that loop might come in handy some time in the future.
            Agreed, the For Each technique can be useful at times. Definitely one to add to the bag of tricks.

            Comment

            Working...