Create Controls Dynamically (VB6)

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.

screen-shot


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
'


Comments

  • Adding Dynamic TextBox with Array Control & Events

    Posted by Carlos Fortin on 05/01/2013 07:45am

    Legacy, just want to let you know that your answer Posted on 09/11/2003 12:00am After all this time is helping me so very much. Thank you,

    Reply
  • 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.

    Reply
  • help me urgently

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

    how to create form dynamically

    Reply
  • how to create menu control in VB?

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

    Hi, 
    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.

    Reply
  • 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 ------------------------------------------

      Reply
    Reply
  • 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
    Else
    .Top = ctlOpt(i - 1).Top + 500
    End If
    .Left = 600
    .Caption = .Name
    .Visible = True
    End With
    Next
    Now I need fire event on each option button. It is possible with one control but not with array control

    Reply
  • Dynamic Control Array

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

    Originally posted by: Deen Bandhu

    Thanks.
    
    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

    Reply
  • 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.

      Reply
    • 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

      Reply
    Reply
  • 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
    -copy
    -paste
    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.

    • Adding Dynamic TextBox with Array Control & Events

      Posted by Carlos Fortin on 05/01/2013 07:47am

      Hello Legacy, I just want you to know that your answer to this questions after all this time is still helping people. It is helping me a lot. Thank you very much!

      Reply
    • 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! :)

      Reply
    Reply
  • Create Controls Dynamically (VB6)

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

    Originally posted by: stewart

    This is fine, but does anyone know how to destroy the controls (equivalent to FreeAndNil in Delphi) - not just setting their .visible property to False?
    

    • Use Dynamically created and destroyed controls to avoid 255 conrol limit?

      Posted by hbielich on 02/21/2006 08:35pm

      Will the use of dynamically created and removed controls work to avoid the 255 control limit for a form?

      Reply
    • Re: destroy the controls

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

      Controls.Remove ("cmd1")

      Reply
    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds