devxlogo

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist