Create New Controls at Run Time

Create New Controls at Run Time

This undocumented feature of VB6 lets you create new controls at run time. In previous versions, one had to create a control array at design time in order to be able to add new controls in run time, which would then be the members of the control array. But in VB6, you can do it without a control array.
Paste following code in the Form’s code window:

 Option ExplicitDim WithEvents mctlCommanButton As VB.CommandButtonPrivate Sub Form_Load()    Set mctlCommanButton = Me.Controls.Add(VB.CommandButton,CommandButton1)        mctlCommanButton.Caption = Hi    mctlCommanButton.Left = 10    mctlCommanButton.Top = 10    mctlCommanButton.Visible = TrueEnd SubPrivate Sub mctlCommanButton_Click()    MsgBox HiEnd Sub

Similarly, you can create any control. For example, for a Timer control, use VB.Timer instead of VB.CommandButton in the above code.


Share the Post: