yeggstry
asked on
Creating objects on a form using Form_Load.....
Ok, this one is a bit harder.
I want 31 new labels for my form, with the values 1 - 31. I know there must be an easier way to add them other then manually enter them in, so I thought I would ask the experts!!
I appreciate any help
Lewis Keen
a.k.a Yeggstry
I want 31 new labels for my form, with the values 1 - 31. I know there must be an easier way to add them other then manually enter them in, so I thought I would ask the experts!!
I appreciate any help
Lewis Keen
a.k.a Yeggstry
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Place no controls on the form and use this code in the form load event
Private Sub Form_Load()
Dim i As Integer
Dim ControlX As Control
i = 1
For i = 1 To 31
Me.Controls.Add "VB.Label", "Label" & i, Me
For Each ControlX In Me.Controls
If ControlX.Name = "Label" & i Then
ControlX.Visible = True
ControlX.Top = i * 400
ControlX.Height = 300
ControlX.Width = 1000
ControlX.Caption = ControlX.Name
ControlX.BorderStyle = 1
End If
Next
Next
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim ControlX As Control
i = 1
For i = 1 To 31
Me.Controls.Add "VB.Label", "Label" & i, Me
For Each ControlX In Me.Controls
If ControlX.Name = "Label" & i Then
ControlX.Visible = True
ControlX.Top = i * 400
ControlX.Height = 300
ControlX.Width = 1000
ControlX.Caption = ControlX.Name
ControlX.BorderStyle = 1
End If
Next
Next
End Sub
why dont you try using control Array ?
Although , if you use control array you trap events but if you plan to create controls at run time using Withevents ( withevents has limitations viz in VB6 ie is it doesnt allow arrays) .
Moreoever if you simply replicate controls during runtime with out using withevents - you cannot trap events.
Only thing you can do is to use control array
If you need more info on how to do it , lemme know.
Amit
Although , if you use control array you trap events but if you plan to create controls at run time using Withevents ( withevents has limitations viz in VB6 ie is it doesnt allow arrays) .
Moreoever if you simply replicate controls during runtime with out using withevents - you cannot trap events.
Only thing you can do is to use control array
If you need more info on how to do it , lemme know.
Amit
Rejecting nomulap's answer.
Force accepting chandukb's comment.
costello
Community Support Moderator @ Experts-Exchange
Force accepting chandukb's comment.
costello
Community Support Moderator @ Experts-Exchange
The only way to do it is to add them manually. You could create them at run time using Form1.Controls.Add but that would require resizing, locating, and setting all other properties at run time (which is a pain).