|
-
October 8th, 2001, 08:10 AM
#7
Re: Adding controls at runtime
Here is a method similar to urs' post however the .Controls.Add works only for VB 6.0.
Start a new project. Paste this code into the general declarations section of the form. Run it and you will see a command button appear. The only seriour flaws in this approach is you can not create control arrays in this fashion nor can you dynamically create control names. If you have need to create many controls at run time, there are samples available using classes or callbacks on Planet-Source-Code.com or similar code repository sites. I also have a couple of examples of creating "pseudo" control arrays but they also only work with VB 6.0.
Code:
option Explicit
' Declare object variable as CommandButton.
private withevents cmdObject as CommandButton
private Sub Form_Load()
set cmdObject = frmCreate.Controls.Add("VB.CommandButton", "cmdOne")
cmdObject.Visible = true
cmdObject.Caption = "Dynamic Command Button"
cmdObject.Move 1000, 1000, 1000, 1000
End Sub
private Sub cmdObject_Click()
print "This is a dynamically added control"
End Sub
private Sub Form_Unload(Cancel as Integer)
' get rid of created control
me.Controls.Remove cmdObject
End Sub
John G
Last edited by Cimperiali; August 12th, 2004 at 08:11 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|