8 Years
Discussion Span
Last Post by Vega_Knight

Try This :
Declaration :

Option explicit

' Contrl variable
Private ctlName As Control

Code :

' This program demonstrates how you can add and delete
' controls on your form dynamically.

' Creates text box and label.
Private Sub cmdCreate_Click()
   ' Creates label.  controls.Add function takes three variables.
   ' first ("vb.label") accepts what type of control you want created,
   ' second ("lblLabel1") accepts the name of the control, the last
   ' one (frmControl) accepts where you want this control to be placed.
   Set ctlName = frmControl.Controls.Add("vb.label", "lblLabel1", frmControl)
   ' Make controls visible.  This step is required if you want the control
   ' to be visible.
   ctlName.Visible = True
   ' Move positions the control requires four arguments
   ' (left, top, width, height)
   ctlName.Move 500, 500, 1500, 250
   ctlName.Caption = "Label 1"
   Set ctlName = _
  frmControl.Controls.Add("vb.textbox", "txtTextBox", frmControl)
   ctlName.Visible = True
   ctlName.Move 2000, 500, 1500, 250

End Sub
' Quits the program.
Private Sub cmdExit_Click()
End Sub

' Removes the controls created in the cmdCreate_Click()
' procedure.
Private Sub cmdRemove_Click()
   frmControl.Controls.Remove "txtTextBox"
   frmControl.Controls.Remove "lblLabel1"
End Sub
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.