Results 1 to 4 of 4

Thread: Dynamic control addition

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Carolina, Puerto Rico, USA
    Posts
    227
    Lets say that i make a program that creates lots of dynamic controls,(not really lots, but more like 5 or 6),using something like this...

    'an if statement goes here

    ControlName = InputBox("Give me the control name.", "Control Name")
    Set Boton = frmDropWindow.Controls.Add("VB.CommandButton", ContolName)
    Boton.Visible = True
    Boton.Caption = InputBox("Give me the caption.", "Caption")
    Boton.Move X - globalX, Y - globalY

    Is there a way i can control every one of the new buttons?
    maybe an array??

    thanks for yout time!!

    [Edited by nievesj on 11-17-2000 at 11:34 PM]
    NievesJ

  2. #2
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    If you are using VB 6 try this:

    Code:
    -------------------------------------------------
    ¡®The following code creates a commandbutton in runtime.
    ¡®If you like you can assign values from Textboxes to the properties.
    ¡®This cannot be applied to object arrays.

    Option Explicit

    Private WithEvents cmdMyButton as CommandButton

    Private Sub Form_Load()

    Set cmdMyButton = Controls.Add(¡°VB.CommandButton¡±,¡°Button¡±)
    With cmdMyButton
    .Visible = True
    .Enabled = True
    .Appearance = 1
    .Width = 3000
    .Height = 500
    .Top = 600
    .Left = 800
    .Caption = ¡°Button Created in runtime¡±
    End With

    End Sub

    Private Sub cmdMyButton_Click()

    MsgBox ¡°Hi! Button has been created successfully.¡±

    End Sub
    -------------------------------------------------

    If you are using VB 5 try this:

    Code:
    ------------------------------------------------
    ¡®Draw a control (a CommandButton, for example) in advance (in design time).
    ¡®Set its Index as 0. Name it, say, cmdMyButton.
    ¡®The following code creates other 4 CommandButtons similar to cmdMyButton. ¡°700¡± is the vertical space between buttons.
    'Again you can assign any values from your textboxes to the properties of the buttons

    Dim i As Integer

    For i = 1 To 4
    Load CmdMyButton(i)
    With CmdMyButton(i)
    .Top = CmdMyButton(i - 1).Top + 700
    .Left = CmdMyButton(i ¨C 1).Left
    End With
    Next
    -------------------------------------------------

    Hope it helps.

  3. #3
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362

    doing it with arrays...

    You need a seed control (not sure if that's true anymore in 6). Start with one textbox and set it's index to 0 (zero)
    Then use the load method to create control number max(index) +1. The normal event handlers for controls will include the index of the paticular control.

    eg. On Form1 there is a TextBox of index 0
    Code:
    Public Sub AddMore
      Load TextBox1(1) '/ create a new text box with index 1
      Load TextBox1(2) '/ create a new text box with index 2
    End Sub
    Then...

    Code:
    Private Sub TextBox1_Change(Index as Integer) '/ Notice the Index already automaticaly added to the definition. Applies for all control methods ( _Click etc)
      MsgBox TextBox1(Index).Text
    End Sub
    Is this what your after?



    td.



    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    tumblingdown@hotmail.com

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  4. #4
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Good. Tumblingdown's code is also true of my second code, like this:

    Code:
    ------------------------------------
    Private Sub cmdMyButton_Click(Index As Integer)

    MsgBox "Hi. This is an Object Array."

    End Sub

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