How can i add a new control to a form and set its settings? Like say i want to add a command button that's 100x100 and located at (100,100), how exactly would id o this... if i can?
Printable View
How can i add a new control to a form and set its settings? Like say i want to add a command button that's 100x100 and located at (100,100), how exactly would id o this... if i can?
I don't think you can.The best you can do is have a command button already on the form and have it's visible property set to false.When whatever you want happens and you want to create a command button,just set the property to visible and set the top,left properties to 100.
VB Code:
Option Explicit Dim WithEvents cmd As CommandButton Private Sub AddButton() Set cmd = Controls.Add("VB.CommandButton", "cmdHello") cmd.Caption = "Hello" cmd.Width = 100 cmd.Height = 100 cmd.Visible = True cmd.Move 100, 100 End Sub
Hey pnish, that's cool!
But how would you call other controls?
vb.Image
vb.Picturebox
...?
I am just guessing...
Where could we know more about that?
if you search in the registry for VB.CommandButton there should also be others that youu can use.
Just make the appropriate changes from command button to label
ie :
VB Code:
Option Explicit Dim WithEvents lbl As Label Private Sub AddButton() Set cmd = Controls.Add("VB.Label", "lblHello") lbl.Caption = "Hello" lbl.Width = 100 lbl.Height = 100 lbl.Visible = True lbl.Move 100, 100 End Sub
I'm not sure but I think you can use this method with most controls. You need to Dim them using WithEvents so you can respond to their events.Quote:
But how would you call other controls?
vb.Image
vb.Picturebox
Try looking in MSDN for Controls Collection and doing a little experimenting.