-
I need some help please, what I am wanting to do is to create command buttons dynamically at run time using the CreateObject method.
I know you can create other buttons and controls by using a control index and loading the next index, but I am wanting to know if you can directly use the CreateObject method.
Now I have created a command button object, however is does not have all the methods and properties available to it as a button made in the editor. I have done this by doing something like:
Dim b as Object
Set b = CreateObject("Forms.CommandButton.1")
However, now I am stuck, how do I get this to display on the form and assign events and change the properties of the button?
Now I think what is happening is when you create a command button in the VB editor MS automatically wraps the class into a usercontrol so creating an ActiveX control that the class is contained in. I think this is the case as looking at the class it has a picture property that you can assign the button.bmp to it, therefore giving it the appearance of a cmdbutton.
So after all of that what I want to know is I am correct in my thinking and if so can I replicate what MS do in the Visual Editor, or I am better off just putting a command button into an ActiveX control myself and then use that as the creation object??
Thanks in advance
Lee
-
People with these crazy ideas again. You're going to have to write the code for this command button any way, unless you know a way to dynamically create code at run time, so the only "advantage?" of using create object is the very slight bit of memory that will not be used until you load this command button.
Crazy :confused:
Code:
Dim WithEvents b As CommandButton
'syntax = object.Add (ProgID, name, container)
Set b = Me.Controls.Add("vb.CommandButton", "b", Me)
b.Visible = True
-
Actually i was just thinking about my last post, and realised there is no advantage to dynamically creating a command button.
As soon as you declare a variable, Visual Basic allocates said variable a space in memory. So even if you don't use this command button, but have it declared, the memory will be set aside any way.
Crazier :confused:
-
Try something like this:
Code:
Option Explicit
Private WithEvents cmdButton1 As VB.CommandButton
Private Sub cmdButton1_Click()
MsgBox "Hello Iconnor"
End Sub
Private Sub Form_Load()
Set cmdButton1 = Controls.Add("VB.CommandButton", "cmdButton1")
cmdButton1.Caption = "Click Me"
cmdButton1.Top = 10
cmdButton1.Left = 20
cmdButton1.Visible = True
End Sub
The WithEvents keyword in the declaration, will add the button to the Object combobox in your code window.
You can use every event, and method available.