Create controls in run time

Posted on 2001-06-26
Last Modified: 2010-05-02

Is there a way to create dynamically (in Run time) controls without putting them at design time first as control arrays?

Question by:amavr
LVL 43

Accepted Solution

TimCottee earned 100 total points
ID: 6227312
Only if you want individual controls, you cannot (AFAIK) create arrays of controls dynamically.

This example from MSDN shows how.

Add Method (Controls Collection) Examples
Private Sub Form_Load()
   Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
   With Form1!cmdObj1
      .Visible = True
      .Width = 2000
      .Caption = "Dynamic Button"
   End With
End Sub

Note   The code example above uses ! as a syntax element. You can also use standard collection syntax such as Form1.Controls("cmdObj1") to reference the control.

This second example declares an object variable of type CommandButton using the WithEvents keyword, allowing you to program the events of the control. The object variable is set to the reference returned by the Add method. To try the example, paste the code into the Declarations section and run the project.

Option Explicit
Private WithEvents btnObj As CommandButton

Private Sub btnObj_Click()
   MsgBox "This is a dynamically added button."
End Sub

Private Sub Form_Load()
   Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
   With btnObj
      .Visible = True
      .Width = 2000
      .Caption = "Hello"
      .Top = 1000
      .Left = 1000
   End With
End Sub

The third example adds an unreferenced control to the Controls collection. To program such a control's events, however, you must declare an object variable of type VBControlExtender, and set the reference returned by the method to the object. Then program the control's events using the ObjectEvent event.

Option Explicit
Dim ctlExtender As VBControlExtender

Private Sub Form_Load()
   Set ctlExtender = Controls.Add("Project1.UserControl1", "MyControl")
   With ctlExtender
      .Visible = True
      .Top = 1000
      .Left = 1000
   End With
End Sub

Private Sub extObj_ObjectEvent(Info As EventInfo)
   ' Program the events of the control using Select Case.
   Select Case Info.Name
   Case "UserName"
      ' Check User name value.
      MsgBox Info.EventParameters("UserName").Value
   ' Other cases now shown
   Case Else ' Unknown Event
      ' Handle unknown events here.
   End Select
End Sub


Expert Comment

ID: 6230366
If you want an array of controls you will need an initial control to create from.  This is often referred to as SPAWNING or CLONING.

First create a new project and add a command button to the form.  Set the index property of this command button to 0 (Zero).  Then add the following code.

Private Sub Command1_Click(Index As Integer)
    Dim NewIndex As Long
    NewIndex = Command1.Count
    Load Command1(NewIndex)
    'The new control is created in the same position as the original
    'so we need to position it.  In this case underneath the original
    Command1(NewIndex).Top = Command1(NewIndex - 1).Top + Command1(NewIndex - 1).Height
    'New controls also start off invisible
    Command1(NewIndex).Visible = True
End Sub

Hope this is of some help and is not patronising.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question