| | Visual Basic 6.0 allows you to dynamically add control to a form at run- time using the new Add method of the Controls collection. This article shows how to dynamically add intrinsic and ActiveX controls. 
 Control     ProgID==========================================
 CheckBox    VB.CheckBox
 ComboBox    VB.ComboBox
 CommandButton  VB.CommandButton
 Frame      VB.Frame
 HScrollBar   VB.HScrollbar
 Image      VB.Image
 Label      VB.Label
 Line      VB.Line
 ListBox     VB.ListBox
 OptionButton  VB.OptionButton
 PictureBox   VB.PictureBox
 Shape      VB.Shape
 TextBox     VB.TextBox
 Timer      VB.Timer
 VScrollBar   VB.VScrollBar
 
 In fact, however, you can add any ActiveX control to your form dynamically, as long as you have the correct ProgID. As I mentioned, ProgIDs are defined in the registry and take the general form LibraryName.Control_Name
 
 If you don't know quite where to search in HKEY_CLASSES_ROOT, you can use the Object Browser to get the correct ProgID. Just add the control to your project, open Object Browser, and select the control name in the classes list box. The Object Browser's status pane will then display the control name and the library name. For instance, Figure 1 shows that the ProgID for the TreeView control is MSComctlLib.Treeview
 
 The following example dynamically adds two intrinsic and one ActiveX control to an application at run-time. The sample shows how to program the events of a dynamically added control. If you are dynamically adding a control that is not referenced in the project, you may need to add the control's License key to the Licenses collection. For more information on the Licenses collection, please see the REFERENCES section of this article.
 
 Step-By-Step Example
 
 - Create a new Standard EXE project. Form1 is created by default.
 - Add the following code to the code window of Form1:
 | 
 |  Click here to copy the following block |  | Option Explicit 
 
 
 Dim WithEvents ctlDynamic As VBControlExtender
 Dim WithEvents ctlText As VB.TextBox
 Dim WithEvents ctlCommand As VB.CommandButton
 
 Private Sub ctlCommand_Click()
 ctlText.Text = "You Clicked the Command button"
 End Sub
 
 Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
 
 If Info.Name = "Click" Then
 ctlText.Text = "You clicked " & ctlDynamic.object.selecteditem.Text
 End If
 End Sub
 
 Private Sub Form_Load()
 Dim i As Integer
 
 
 
 Licenses.Add "MSComctlLib.TreeCtrl"
 
 
 
 
 
 Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
 "myctl", Form1)
 
 ctlDynamic.Move 1, 1, 2500, 3500
 
 
 For i = 1 To 10
 ctlDynamic.object.nodes.Add Key:="Test" & Str(i), Text:="Test" _
 & Str(i)
 ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), _
 Relationship:=4, Text:="TestChild" & Str(i)
 Next i
 
 
 ctlDynamic.Visible = True
 
 
 Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
 
 ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
 1, 2500, 100
 
 
 ctlText.BackColor = vbYellow
 
 
 ctlText.Visible = True
 
 
 Set ctlCommand = Controls.Add("VB.CommandButton", _
 "ctlCommand1", Form1)
 
 
 ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
 ctlText.Height + 50, 1500, 500
 
 
 ctlCommand.Caption = "Click Me"
 
 
 ctlCommand.Visible = True
 End Sub
 | 
 | - Save and run the project. Try clicking the CommandButton and on different Nodes in the TreeView. The TextBox will show what you click. 
 More information
 
 Now we will walk through VBControlExtender Object, ObjectEvent Event, EventParameter Object Examples which are important to know when you deal with dynamic controls.
 
 VBControlExtender Object : Represents the Visual Basic VBControlExtender properties.
 
 The VBControlExtender object is primarily used when dynamically adding a control to the Controls collection using the Add method. The VBControlExtender object is particularly useful for this purpose because it provides a generic set of properties, events, and methods to the developer. Another feature of the object is the ObjectEvent event which is designed to parse any event raised by a dynamically added control. The example below declares an object variable as VBControlExtender, and sets the variable when adding a control. The example also shows how you can program the ObjectEvent event.
 
 Restrictions on Setting the References to the Variable
 There is one caveat to be aware of when setting the VBControlExtender object to a dynamically added control: intrinsic controls cannot be set to the variable.
 
 Here is the example
 | 
 |  Click here to copy the following block |  | Option Explicit Dim WithEvents objExt As VBControlExtender
 
 Private Sub LoadControl()
 Licenses.Add "Project1.Control1", "ewrinvcmcoe"
 Set objExt = Controls.Add("Project1.Control1", "myCtl")
 objExt.Visible = True
 End Sub
 
 Private Sub extObj_ObjectEvent(Info As EventInfo)
 
 Select Case Info.Name
 Case "Click"
 
 
 Case Else
 
 End Select
 End Sub
 | 
 | ObjectEvent Event : Represents event information raised by a control assigned to a VBControlExtender object variable. 
 The EventInfo object is available in the ObjectEvent event, which is an event of the VBControlExtender object. In common practice, a control that is dynamically added to the Controls collection using the Add method will be assigned to an object variable of the type VBControlExtender. The ObjectEvent event can then be used to trap all events raised by the control, and the EventInfo object specifically represents any parameters passed by the raised events.
 
 The EventInfo object has two properties: the Name property returns the name of the raised event; the EventParameters property returns a reference to the EventParameters collection that allows you to return values of all event parameters.
 
 ObjectEvent Event : Occurs when a control that is assigned to a VBControlExtender object variable raises an event.
 
 Syntax : Private Sub object_ObjectEvent(Info As EventInfo)
 
 The ObjectEvent event syntax has these parts:
 
 Part Description
 
 object Required. An object expression that evaluates to an object in the Applies To list.
 Info Returns a reference to an EventInfo object.
 
 Remarks
 
 The ObjectEvent event is a generic event that allows you to handle events of a control and return values through the control's event parameters.
 
 You can use the ObjectEvent event to trap generic events a control raises, assuring that any control you deploy contains certain basic functionality required by the deployed application.
 
 Here is the ObjectEvent Event, EventParameter Object Examples
 
 The first example below uses the ObjectEvent event to print all parameter names and values.
 | 
 |  Click here to copy the following block |  | Option Explicit Private WithEvents extObj As VBControlExtender
 
 
 
 Private Sub extObj_ObjectEvent(Info As EventInfo)
 Dim p As EventParameter
 Debug.Print Info.Name
 
 For Each p In Info.EventParameters
 Debug.Print p.Name, p.Value
 Next
 End Sub
 | 
 | The second example allows you to check for a generic event raised by the control. | 
 |  Click here to copy the following block |  | Private Sub extObj_ObjectEvent(Info As EventInfo) Dim p As EventParameter
 Select Case Info.Name
 Case "UserName"
 
 MsgBox Info.EventParameters("UserName").Value
 
 Case Else
 
 End Select
 End Sub
 | 
 |