Results 1 to 7 of 7

Thread: Array Of Controls

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Hi,

    In vb if i do Controls.Add, that works fine except that if i want to make an array of controls then it doesnt work, how can i create an array like btnOk(1)

  2. #2
    Guest
    See if this works. (I can't test it because I don't have VB6)
    Code:
    Private Sub Command1_Click()
        Controls.Add "VB.Label", "Label1"
        Me!Label1.Move 0, 0
        Me!Label1.Caption = "Label1"
        Me!Label1.Visible = True
        Me!Label1.Index = 0
    
        'Now add more controls with the same name.
        Controls.Add "VB.Label", "Label1"
        Controls.Add "VB.Label", "Label1"
    End Sub

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    In fact you can't create control arrays at runtime.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Try This


    Use the load() statement to load a control in an array.

    code:
    Private Sub Command1_Click()
    dim iLoop as Integer
    Controls.Add "VBLabel", "Label1"
    Label1.index = 0

    'Creates an array of 20 controls.
    for iLoop = 1 to 1
    Load Label1(iLoop)
    next iLoop
    End sub
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  5. #5
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    No can do

    Wak, in your coding you have "Label1.index = 0". This will cause an error and your application will not compile, because there is no Label1 yet.

    The only way you can do this is by adding one control during design time, then you can load as many as you want during runtime.

    Add the control you need, set the index property to 0, thus making it an array, and then you can load instances as you need during runtime.

    Code:
      'create 2 more textboxes
      Load txtTest(1)
      Load txtTest(2)
    
      'use the next index in line
      Load lblPrompt(lblPrompt.UBound + 1)
    Hope this helps.
    Shrog

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Ok, i can add controls at design time, do u think that is the best way (using the load statement) ?

  7. #7
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    Smile

    I don't know if it's the best way, but it's the best way I know. I use this all the time, and we use it in the commercial software that my company produce.

    Shrog

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