Results 1 to 6 of 6

Thread: Creating control arrays with nested components

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    551

    Creating control arrays with nested components

    I have an array of pictureboxes. I create the first picturebox at design time (by "first" I mean the one with its index being zero), and the rest of them like this at run-time:
    Code:
       For i = 1 To 2
          Load Picture4(i)
          
          Picture4(i).Top = Picture4(i - 1).Top + Picture4(i - 1).Height + 100
          Picture4(i).Visible = True
       Next i
    This works without a hitch. It creates the desired pictureboxes as expected.

    But, the problem is if I also have an array of labels, the first member of which is nested inside the first picturebox (again, by "first" I mean the one with its index being zero).
    Then when I try to create the rest of labels at run-time, they are created inside the first picturebox (instead of each one being created inside its corresponding picturebox).

    Even this doesn't help:
    Code:
          lblFileNum(i).Container = Picture4(i)
    Here is the full code:
    Code:
       For i = 1 To 2
          Load Picture4(i)
          Load lblFileNum(i)
    
          lblFileNum(i).Container = Picture4(i)
          lblFileNum(i).Visible = True
          
          Picture4(i).Top = Picture4(i - 1).Top + Picture4(i - 1).Height + 100
          lblFileNum(i).Top = lblFileNum(i - 1).Top + lblFileNum(i - 1).Height + 100
          Picture4(i).Visible = True
       Next i
    And here is the result:
    https://i.imgur.com/LDnD6KG.jpg

    How can I load the two control arrays properly (each label falling within its corresponding picturebox)?
    Please help.
    Thanks

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,304

    Re: Creating control arrays with nested components

    Set Label.Container = Picture4

    EDIT1: I'm not sure what the default property of the PictureBox is, but that's all you're changing with the way you've got it.

    EDIT2: After a bit of research, it appears the default property of a PictureBox is the Picture object. However, that's also an object, and since you weren't using Set, that's not what was being changed either, so we dig deeper. The default property of the Picture object is its handle. And that's what you were changing. If any of your PictureBoxes had actually had a picture in them, you would have seen the change.
    Last edited by Elroy; Oct 12th, 2019 at 07:34 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.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    496

    Re: Creating control arrays with nested components

    Just guessing but I don't think you need this code line:

    lblFileNum(i).Top = lblFileNum(i - 1).Top + lblFileNum(i - 1).Height + 100

    When you Set lblFileNum(i).Container = Picture4(i) the label goes into Picture4 at the same location it was in your original Picture4 so it is already correctly positioned

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,304

    Re: Creating control arrays with nested components

    Ilia,

    Also, maybe I should have said this before, you may want to check out the Controls.Add method. That allows you to add controls from scratch (without your "first" control). There's nothing wrong with the way you did it, but sometimes I like just doing it from scratch, as it seems cleaner.

    There are tons of examples of using this Controls.Add method on these forums.

    Good Luck,
    Elroy
    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
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    551

    Re: Creating control arrays with nested components

    Quote Originally Posted by Ordinary Guy View Post
    Just guessing but I don't think you need this code line:

    lblFileNum(i).Top = lblFileNum(i - 1).Top + lblFileNum(i - 1).Height + 100

    When you Set lblFileNum(i).Container = Picture4(i) the label goes into Picture4 at the same location it was in your original Picture4 so it is already correctly positioned
    You are definitely right.
    But, just in order to present the problem properly and visibly to everybody on this forum (via that screen print), I added that redundant line of code. (just for showing the problem).
    Thanks.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    551

    Re: Creating control arrays with nested components

    Quote Originally Posted by Elroy View Post
    Ilia,

    Also, maybe I should have said this before, you may want to check out the Controls.Add method. That allows you to add controls from scratch (without your "first" control). There's nothing wrong with the way you did it, but sometimes I like just doing it from scratch, as it seems cleaner.

    There are tons of examples of using this Controls.Add method on these forums.

    Good Luck,
    Elroy
    That "Set" statement that you recommended, definitely solved the problem.
    Thanks a lot for that.

    I will also explore your new suggestion (Controls.Add). Who knows; It may be even a better way of creating controls at run-time.
    Thanks again.
    Ilia

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