Add new control in form at run-time


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Add new control in form at run-time

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Add new control in form at run-time

    [Originally posted by Elias Gregory]

    For my application, I need to add new picture in a form (for example with a Commandbutton click) during run-time. My first idea, was to use "as new" and a pointer to stack the new picture. It's ok for a form but it seems not correct for a control...
    Thanks for your help (and sorry for my poor english)

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Add new control in form at run-time

    [Originally posted by Greg DeBacker]

    You can use a control array and add new controls to the array at runtime. Here is an example.

    Add a PictureBox and a CommandButton to a new form. Set the PictureBox.Index = 0 in the properties window. This will initialize the array.

    Paste the code below in the forms declarations section. Press F5 to run the program. When you click the command button you will create new pictureboxes.

    Option Explicit
    Dim iNewIndex As Integer

    Private Sub Command1_Click()
    iNewIndex = iNewIndex + 1
    ScaleMode = vbTwips
    Load Picture1(iNewIndex)
    Picture1(iNewIndex).Visible = True
    Picture1(iNewIndex).Left = Picture1(iNewIndex - 1).Left + 500
    Picture1(iNewIndex).Top = Picture1(iNewIndex - 1).Top + 500
    End Sub


    Greg

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Add new control in form at run-time

    [Originally posted by Lee Seddon]

    You could use a control array and then dynamically add a new control at run time, below adds 5 new command buttons at run time. First add a command button placing it at the top of the form and name it "cmdButton", then change it's index property to 0, insert ‘Show’ in the Tag property of the command button and finally paste the code and run.

    '***********************************************
    Private Sub cmdbutton_Click(Index As Integer)

    Dim Counter As Integer

    Select Case cmdButton(0).Tag

    Case "Show"

    For Counter = 1 To 5

    cmdButton(0).Tag = "Hide"
    Load cmdButton(Counter)

    With cmdButton(Counter)
    .Top = cmdButton(Counter - 1).Top + cmdButton(Counter - 1).Height
    .Caption = "Button " & Counter
    .Tag = ""
    .Visible = True
    End With

    Next

    Case "Hide"

    If Index > 0 Then
    MsgBox "You pressed button " & Index
    Exit Sub
    End If

    cmdButton(0).Tag = "Show"

    For Counter = 1 To 5
    Unload cmdButton(Counter)
    Next

    End Select

    End Sub

    '************************************************

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Add new control in form at run-time

    [Originally posted by Elias Gregory]

    Thank you very much for your help. It was very easy. I have to be in a brainstorming period.

    Gregory

  5. #5
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Add new control in form at run-time

    [Originally posted by Elias Gregory]

    Thanks for your help. It was very easy.

    Gregory

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center