• Adding Control Array (VB6)

    Author
    Topic
    #394431

    Hi All,

    Does anybody know of a way to add a control ARRAY at runtime?
    I’m adding a Line control, but want it to be a control array.
    Here’s how I’m adding the line: Me.Controls.Add “VB.Line”, “lineXXX”
    This works great, but can’t make it an array or give it an index property.
    Any ideas? I’m not averse to using the API.
    Thanks,

    Viewing 1 reply thread
    Author
    Replies
    • #737922

      I don’t know if you are still looking for an answer to this question.

      It is not possible to create a control array from scratch entirely in code. You will need to create the first element in design mode and set its Index property to 0. This item can be hidden if you wish.

      Say that you have created a line called MyLine with index 0 in design mode. You can create new elements as follows:

      Dim i As Long
      For i = 1 To 10
      Load MyLine(i)
      MyLine(i).Y1 = MyLine(0).Y1 + 144 * i
      MyLine(i).Y2 = MyLine(0).Y2 + 288 * i
      MyLine(i).Visible = True
      Next i

      • #738018

        Thanks for the response HansV. I arrived at the same conclusion myself. I was hoping there would be a slick API way to do it.

        Ever see one of those programs where you have 2 listboxes full of items, and you need to equate an item in one with an item in the other? This is usually done for data mapping purposes. You click on an item in each listbox, tap a button, and a line appears between them. Well, I wrote a VB6 class to manage that task, and I wanted the user to not have to add a line to his form, but rather the class would do it all. Alas, no luck. The user only needs a form with 2 listboxes, a line array and link/unlink buttons, and it works. If anybody is interested, reply or e-mail me and I’ll post a sample project.

        Thanks again for trying.

        • #806228

          I was going to create a new post for this but couldn’t work out how to link to posts, therefore a reply will have to do for now:

          Using Hans’ method of adding controls to a control array, does anyone know how to remove controls from the array? I’d like to remove all of the controls so either a complete “destructor” for all runtime made controls of that name or a single remove function that I can loop through the array’s index will do.

          I found the frm.object.remove (“obj name”) command, but couldn’t get it to work with an array.

          Any help would be much appreciated.

          Regards,

          • #806240

            1. Each post has a number, displayed below the subject. for instance, the post at the top of this thread has number 298772. You can create a link to it in two ways (also see Help 19):

            [post#=298772] results in post 298772
            [post=Adding control arrays,298772] results in Adding control arrays

            2. Just as you can use Load MyLine(i) to create a new instance, you can use Unload MyLine(i) to destroy an existing instance. Using the same example as in my reply higher up in this thread:

            Dim i As Long
            For i = 10 To 1 Step -1
            Unload MyLine(i)
            Next i

            • #806242

              Wow, thanks Hans, both problems solved in one hit.

              Next time I need to link to such a post as Adding control arrays it will be no trouble at all.

              The removing arrays code works like a treat, and was so easy to implement!

              Thanks for your help, and such speedy help it was too!

            • #806243

              Wow, thanks Hans, both problems solved in one hit.

              Next time I need to link to such a post as Adding control arrays it will be no trouble at all.

              The removing arrays code works like a treat, and was so easy to implement!

              Thanks for your help, and such speedy help it was too!

          • #806241

            1. Each post has a number, displayed below the subject. for instance, the post at the top of this thread has number 298772. You can create a link to it in two ways (also see Help 19):

            [post#=298772] results in post 298772
            [post=Adding control arrays,298772] results in Adding control arrays

            2. Just as you can use Load MyLine(i) to create a new instance, you can use Unload MyLine(i) to destroy an existing instance. Using the same example as in my reply higher up in this thread:

            Dim i As Long
            For i = 10 To 1 Step -1
            Unload MyLine(i)
            Next i

        • #806229

          I was going to create a new post for this but couldn’t work out how to link to posts, therefore a reply will have to do for now:

          Using Hans’ method of adding controls to a control array, does anyone know how to remove controls from the array? I’d like to remove all of the controls so either a complete “destructor” for all runtime made controls of that name or a single remove function that I can loop through the array’s index will do.

          I found the frm.object.remove (“obj name”) command, but couldn’t get it to work with an array.

          Any help would be much appreciated.

          Regards,

      • #738019

        Thanks for the response HansV. I arrived at the same conclusion myself. I was hoping there would be a slick API way to do it.

        Ever see one of those programs where you have 2 listboxes full of items, and you need to equate an item in one with an item in the other? This is usually done for data mapping purposes. You click on an item in each listbox, tap a button, and a line appears between them. Well, I wrote a VB6 class to manage that task, and I wanted the user to not have to add a line to his form, but rather the class would do it all. Alas, no luck. The user only needs a form with 2 listboxes, a line array and link/unlink buttons, and it works. If anybody is interested, reply or e-mail me and I’ll post a sample project.

        Thanks again for trying.

    • #737923

      I don’t know if you are still looking for an answer to this question.

      It is not possible to create a control array from scratch entirely in code. You will need to create the first element in design mode and set its Index property to 0. This item can be hidden if you wish.

      Say that you have created a line called MyLine with index 0 in design mode. You can create new elements as follows:

      Dim i As Long
      For i = 1 To 10
      Load MyLine(i)
      MyLine(i).Y1 = MyLine(0).Y1 + 144 * i
      MyLine(i).Y2 = MyLine(0).Y2 + 288 * i
      MyLine(i).Visible = True
      Next i

    Viewing 1 reply thread
    Reply To: Adding Control Array (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: