Results 1 to 8 of 8

Thread: Dynamically creating elements of control array **Resolved**

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352

    Dynamically creating elements of control array **Resolved**

    Here is what I have. I have a form with a PictureBox and a vertical scroll bar. On top of the PictureBox I have a OptionButton that is set to graphical type. It is as follows.

    optButton(0) 'This is the only optButton control drawn on the form at runtime

    It is element 0 of the control array optButton. How can I programatically create the 1,2, and 3 elements of the control array without physically drawing the controls on the form at designtime?

    The elements must be created at run-time and only if needed, that way I can create 1 if needed or 100, whatever the case maybe.

    Thanks.
    Last edited by easymoney; Nov 9th, 2002 at 03:48 AM.

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    VB Code:
    1. Dim i%
    2.  
    3.     For i =1 To 3
    4.         Load Option1(i)
    5.         Option1(i).Move Option1(i-1).Left, Option1(i-1).Top + Option1(i-1).Height
    6.         Option1(i).Visible = True
    7.     Next i
    Roy

  3. #3
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    You will also need to set caption:
    Option1(i).Caption = "Option1" 'or whatever
    Roy

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352
    I only have one control drawn on the form... Index = 0

    So I get this error message:

    Control array element '1' does not exist.

    This is the heart of my problem. How do you create elements 1,2,3...etc at run-time?

  5. #5
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    I just showed you??? Place that code to a Form_Load event and voila ...
    Roy

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352
    OK... its all in the details, makes sense if you want to create controls, do it when the form loads... duh.

    What if you need to redim the control array.... can you force the load event again? (ie, I loaded 20, but now I need 45??)

  7. #7
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    VB Code:
    1. Private Sub Command1_Click() 'or from anywhere in the project
    2. Dim i%
    3.  
    4.     For i =Option1.UBound + 1 To Option1.UBound + 25
    5.         Load Option1(i)
    6.         Option1(i).Move Option1(i-1).Left, Option1(i-1).Top + Option1(i-1).Height
    7.         Option1(i).Visible = True
    8.     Next i
    9. End Sub
    Roy

  8. #8
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Here is a quick sample that you may use throughout your project:
    VB Code:
    1. 'on the form
    2. Option Explicit
    3.  
    4. Private Sub Command1_Click()
    5.     AddMoreControls Option1, 3
    6. End Sub
    7.  
    8. Private Sub Form_Load()
    9.     AddMoreControls Option1, 2
    10. End Sub
    11.  
    12. 'paste this sub into a general module
    13. Public Sub AddMoreControls(ByVal ctl As Object, intNewUbound As Integer)
    14. '========================================================================
    15. Dim i%
    16.  
    17.     For i = ctl.UBound + 1 To ctl.UBound + intNewUbound
    18.         Load ctl(i)
    19.         ctl(i).Move ctl(i - 1).Left, ctl(i - 1).Top + ctl(i - 1).Height
    20.         ctl(i).Visible = True
    21.     Next i
    22.  
    23. End Sub
    Roy

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