Results 1 to 4 of 4

Thread: Adding Controls to Forms at Runtime

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    Christchurch, New Zealand
    Posts
    12

    Question

    Hi is there a way of adding controls to a form at runtime. Example if you decide you want 2 buttons you can create them in your code, or if you want 20 you can do the same. The only way I have thought of so far is to have 20 buttons on the form and if you only want 2 making the other 18 invisible.

    Thanks

  2. #2
    Guest
    You can create controls using a Control Array. For example, place a CommandButton on a form, with the Index property set to 0. This code will loop through and create 20 command buttons.

    Code:
    Private Sub Form_Initialize()
    
    For i = 1 To 20
    Load Command1(i)
    Command1(i).Move 0, 0 + (480 * i)
    Command1(i).Visible = True
    Next i
    
    End Sub
    If you would like to create them from scratch, I know there is a method to do it, but it can only be done in VB6
    I do not know the syntax for it either.

  3. #3
    Guest
    Hold on a sec, I knew I had the syntax for it but I didn't have it on my this computer. I booted up my old P166 computer and found this for you. This method for adding controls is only possible in VB6.

    Make a Form with 1 CommandButton on it. Place the following code into it.

    Code:
    Private Sub Command1_Click()
    
       Controls.Add "VB.CommandButton", "cmdNewButton"
       Me!cmdNewButton.Move 0,0
       Me!cmdNewButton.Caption = "PRESS"
       Me!cmdNewButton.Visible = True
    
    End Sub
    I hope this helps you.

    [Edited by Megatron on 05-18-2000 at 06:20 PM]

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    Christchurch, New Zealand
    Posts
    12

    Cool Thankyou

    Thankyou as I was in a bind

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