Discussion:
add new control to form
(too old to reply)
Greg Hind
2006-09-01 00:13:53 UTC
Permalink
I am trying to add a Label control programatically to a form. I am trying
to create an appilcation in Microsoft Visuals Basic 6.0 and am seeking help.
Ralph
2006-09-01 12:01:44 UTC
Permalink
Post by Greg Hind
I am trying to add a Label control programatically to a form. I am trying
to create an appilcation in Microsoft Visuals Basic 6.0 and am seeking help.
You can't create a control from scratch at runtime. However, you can easily
add a control to a control array. The simplist method is to place a Label on
a form and make it part of an array. (It can be hidden.) Or place all your
current labels in an array.

Then simply add a new label at runtime, setting its properties (location).

You will find additional information in the help files (control arrays).

-ralph
Norm Cook
2006-09-02 13:35:57 UTC
Permalink
Post by Greg Hind
I am trying to add a Label control programatically to a form. I am trying
to create an appilcation in Microsoft Visuals Basic 6.0 and am seeking help.
In addition to Ralph's comments you do this. Note that you can
either use add array elements to an existing (design time) control array
or use the method below, but you can't do both (i. e. dynamically
create a control array)


Option Explicit
Private WithEvents lblObject As Label

Private Sub Form_Load()
Set lblObject = Form1.Controls.Add("VB.Label", "lblOne")
With lblObject
.Visible = True
.Caption = "Dynamic Label"
.Move (Width - .Width) \ 2, (Height - .Height) \ 2
End With
End Sub

Private Sub lblObject_Click()
MsgBox "This is a dynamically added control"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set lblObject = Nothing
End Sub

Loading...