Results 1 to 5 of 5
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015

    Resolved [RESOLVED] Loop through a number of controls

    Hello vbforums experts
    I have a number of picturebox controls on which I'm adding some shapes, labels...
    First I need to track the last selected picturebox. For this I did as follows.
    Dim cntl As Control
    And at the click event of each picture box I set cntl = the selected picture.
    What I'm doing so far is this:
    Private Sub Command1_Click()
    If cntl = Picture1 Then
    Shape1.Visible = True
    Label1.Visible = True
    Line1.Visible = True
    ElseIf cntl = Picture2 Then
    Shape2.Visible = True
    Label2.Visible = True
    Line2.Visible = True
    ElseIf cntl = Picture3 Then
    Shape3.Visible = True
    Label3.Visible = True
    Line3.Visible = True
    ElseIf cntl = Picture20 Then
    Shape20.Visible = True
    Label20.Visible = True
    Line20.Visible = True
    End If
    End Sub
    I'm looking for a way to shorten my code.
    Either by means of arrays or any other method.
    Thank you very much
    Last edited by newbie2; May 16th, 2018 at 05:48 PM.

  2. #2
    Join Date
    Feb 2012
    West Virginia

    Re: Loop through a number of controls

    Well the first thing that comes to mind is to use control arrays Picture1(0-19) Shape1(0-19) and so on for each control used then your code would be.

    Dim PictureIndex as Integer
    Private Sub Command1_Click()
    Shape1(PictureIndex).Visible = True
    Label1(PictureIndex).Visible = True
    Line1(PictureIndex).Visible = True
    You would set PictureIndex to the Index passed into the Picture1_Click event.

    Not only does this make the code simplier but you would only be using 4 unique controls rather than 80 of your 255 limit.
    Last edited by DataMiser; May 16th, 2018 at 05:45 PM.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Beside Waldo

    Re: Loop through a number of controls

    The best solution is to convert your controls into a control array. Then you can loop thru each very easily, i.e.,
    For n = PictureX.LBound To PictureX.UBound
        If PictureX(n) is cntl Then
            ShapeX(n).Visible = True
            LineX(n).Visible = True
            LabelX(n).Visible = True
            Exit For
        End If
    note: for above to work each set of picturebox, label, line & shape must have the same LBound/UBound

    Edited: DataMiser beat me to it

    You can still do this as you have it, but it is less intuitive
    For n = 1 To 20
        If Me.Controls("Picture" & CStr(n)) Is cntl Then
            Me.Controls("Shape" & CStr(n)).Visible = True
            Me.Controls("Line" & CStr(n)).Visible = True
            Me.Controls("Label" & CStr(n)).Visible = True
            Exit For
        End If
    Note: for above to work, obviously you must have similar naming conventions for each set of Picturebox, Shape, Line & Label

    When comparing objects, use IS not =. When you compare Picture1 = cntl, for example, you are comparing their picture property values, not the control itself
    Last edited by LaVolpe; May 16th, 2018 at 05:48 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online

    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Near Nashville TN

    Re: Loop through a number of controls

    Hi newbie2,

    Yes, I wholeheartedly agree with DataMiser and LaVolpe. Control-Arrays are the way to go with something like this. And also, don't confuse Control-Arrays with Arrays-of-Controls. They're very different concepts.

    A Control-Array is when you start using the Index property (on the Properties Window) of a control. By using that, you can have several controls (of the same type, say PictureBox) with the same name, so long as they each have a different Index property value.

    If each PictureBox has a corresponding Line and Shape and whatever, you might be able to synchronize the Index properties of several Control-Arrays. That's what LaVolpe was suggesting in the examples he gave you. Control-Arrays are a great way to reduce the code you showed us in post #1 into a loop.

    Now, regarding that "last selected" problem, there are a couple of ways to address that. If you have synchronized Control-Arrays, you might be able to just save some module-level variable named LastClickedIndex. It would be an Integer. And you could just use that to address the last selected set of controls (from their respective arrays) when you needed to change something.

    Alternatively, you could keep doing it the way you've shown. However, you'd just need to do something like the following:

    Option Explicit
    Dim LastClickedPic As PictureBox
    Private Sub MyPicBox_Click(Index As Integer)
        Set LastClickedPic = MyPicBox(Index)
    End Sub
    And then, if you want, you can say LastClickedPic.Index to figure out what its Index value is, and use that to "get at" other synchronized Control-Arrays.

    Also, notice that when you have Control-Arrays, you always get that Index property handed to you in the events. That's how you know which of the controls in a Control-Array is actually firing the event.

    Good Luck,

    EDIT1: And just to close the loop, an Array-of-Controls is where you'd make some object variable array, and then stuff a bunch of controls in it. That's not really what you want (I don't think). It's a very different concept from Control-Arrays.

    EDIT2: Also, just another passing thought. Don't forget you've got the Controls collection with every form. I'm not sure you need it here, but that gives you a mechanism to loop through every single control that's on a form.
    Last edited by Elroy; May 16th, 2018 at 06:11 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015

    Re: [RESOLVED] Loop through a number of controls

    Thank you experts very much

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.