Create Controls Dynamically (VB6)


Application Security Testing: An Integral Part of DevOps

Author: Manoj Tuli

Create a Control dynamically at Run time. Without using a Control Array. That is the control is not present at Design time But will appear at Run Time.


option Explicit
Dim withevents Cmd1 as CommandButton
private Sub Form_Load()
  set Cmd1 = Controls.Add("vb.commandbutton", "Cmd1")
  Cmd1.Width = 2000
  Cmd1.Top = me.Height / 2 - Cmd1.Height / 2 - 100
  Cmd1.Left = me.Width / 2 - Cmd1.Width / 2 - 100
  Cmd1.Caption = "Dynamic Button"
  Cmd1.Visible = true
End Sub
private Sub Cmd1_click()
  MsgBox "I have been Created Dynamically at Run-time", _
    , "Dynamic Controls"
End Sub


  • ejemplo de array dinamico

    Posted by jorge on 05/17/2017 07:15am

    Public Sub IniciarNodos(PIC As PictureBox, pNumNodos As Long) Dim Maxx As Single Dim Maxy As Single Dim medx As Single Dim medy As Single Dim i As Long Dim pasoy As Single Dim pasox As Single Dim j As Long PIC.ScaleMode = vbPixels PIC.AutoRedraw = True Maxx = PIC.ScaleWidth Maxy = PIC.ScaleHeight medx = Maxx / 2 medy = Maxy / 2 pasox = Maxx / pNumNodos PIC.Line (0, medy)-(Maxx, medy) i = 1 'dibujar nodos inicial For j = 1 To Maxx Step pasox If i 1 Then Load frmWaveEdit.pNodos(i) End If frmWaveEdit.pNodos(i).left = i * pasox frmWaveEdit.pNodos(i).top = medy frmWaveEdit.pNodos(i).Visible = True i = i + 1 Next End Sub

  • Hey you!

    Posted by Alan Balkany on 01/13/2017 02:47pm

    Thanks! This helped!

  • dynamical objects

    Posted by Albert on 04/26/2012 01:16am

    gd morning hve a question regaring dynamically creating of controls using such as 'Set FM1 = Controls.Add("vb.Frame", "Frame1")' posible to use a dynamic name (string) instead of a fixed name? strObjName = "NewObj" & Counter Form1.Controls.Add("vb.textbox", strObjName, Form1) cannot use above system.

  • help me urgently

    Posted by raviraj on 04/27/2004 09:48am

    how to create form dynamically

    • New Form

      Posted by Imran Khan on 07/24/2017 11:38pm

      You can write New Form in your code to create a new form in your VB project.

  • how to create menu control in VB?

    Posted by binhthanh on 04/16/2004 02:16am

    I want to create a menu control(support unicode) but i dont know to create it. Please tell me how to create or sourcecode. Thanks.

  • its really a great stuff how run time button generation

    Posted by Legacy on 12/24/2003 12:00am

    Originally posted by: Pranav Jariwalla

    Well, but how can we add run time buttons in to Frame

    • Re to Pravan Jariwalla: Add Dynamic Button to the frame control.

      Posted by manoj_tuli on 05/11/2004 03:10am

      Friend, Heres the code to do the same. Sorry for the delay in Replying. ------------------------------------------ Option Explicit Dim WithEvents Cmd1 As CommandButton Dim WithEvents FM1 As Frame Private Sub Form_Load() Set FM1 = Controls.Add("vb.Frame", "Frame1") FM1.Top = 150 FM1.Left = 300 FM1.Width = 3000 FM1.Height = 2000 FM1.Caption = "Dynamic Frame" 'FM1.Name = "Frame1" FM1.Visible = True Me.Show DoEvents Set Cmd1 = Controls.Add("vb.commandbutton", "Cmd1") Cmd1.Width = 2000 Cmd1.Top = Me.Height / 2 - Cmd1.Height / 2 - 100 Cmd1.Left = Me.Width / 2 - Cmd1.Width / 2 - 100 Cmd1.Caption = "Dynamic Button" Set Cmd1.Container = FM1 Cmd1.Visible = True End Sub ' Private Sub Cmd1_click() MsgBox "I have been Created Dynamically at Run-time, with Frame 1 as my container.", , "Dynamic Controls" End Sub ------------------------------------------

  • Adding event in dynamic control

    Posted by Legacy on 12/18/2003 12:00am

    Originally posted by: Deen Bandhu

    How to add event on dynamically created control.
    I have tried with this code.

    For i = 1 To 2
    Set ctlOpt(i) = Form1.Controls.Add("VB.OptionButton", "Opt" & i, ctlFrm(1))
    With ctlOpt(i)
    If i = 1 Then
    .Top = 100
    .Top = ctlOpt(i - 1).Top + 500
    End If
    .Left = 600
    .Caption = .Name
    .Visible = True
    End With
    Now I need fire event on each option button. It is possible with one control but not with array control

  • Dynamic Control Array

    Posted by Legacy on 12/08/2003 12:00am

    Originally posted by: Deen Bandhu

    Finally I got the code for dynamic control Arry.

    But one problem is still there how to dispaly multiple control arryay say text box in limited space of form.

    For that i need a scrollable container like in html page to vied n number of control.

    Plz Help me

  • Help me urgently

    Posted by Legacy on 10/28/2003 12:00am

    Originally posted by: arun prakash

    The code was nice & simple.But I want to know how to write events.I have create an array of textboxes.PLzzzzzzzz help me .Thanks in advance

    • Re: Help me urgently

      Posted by manoj_tuli on 05/11/2004 03:25am

      Please send me your code on thsi address ping_manoj@hotmail.com. Let me see what exaclt are w etalking about.

    • Help me urgently

      Posted by raviraj on 04/26/2004 09:53am

      The code was nice & simple.But I want to know how to write events.I have create an array of textboxes.PLzzzzzzzz help me .Thanks in advance

  • Adding Dynamic TextBox with Array Control & Events

    Posted by Legacy on 09/11/2003 12:00am

    Originally posted by: aditul

    On a blank form

    -create textBox
    -rename it to arrayText
    -selct the textBox
    You'll be asked if you want to create a control array 'cause you have another textBox with the same name
    -click ok
    you have now the array control created.
    you can delete the second now.
    you'll have one control array with only one control

    -fill in this code in Form_Load()

    For i = 1 To 10

    Load arrayText(i)
    arrayText(i).Left = arrayText(0).Left
    arrayText(i).Top = arrayText(0).Top + 300 * i
    arrayText(i).Text = "array textbox dynamic (" & i & ")"
    arrayText(i).Width = 3500
    arrayText(i).Height = 285
    arrayText(i).Visible = True

    MsgBox "textbox dynamic nr " & i, vbOKOnly + vbInformation, "Created???"

    Next i

    Run it, should work.

    • VB Dynamic Controls

      Posted by Peter on 04/23/2012 04:15am

      I needed to add a 'number' of controls, and just couldn't get my head around the syntax. This article worked first time! :)

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.