Results 1 to 4 of 4

Thread: CreateObject - Command buttons

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    1

    Post

    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




  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    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

    Code:
    Dim WithEvents b As CommandButton
    
    'syntax = object.Add (ProgID, name, container)
    Set b = Me.Controls.Add("vb.CommandButton", "b", Me)
    b.Visible = True
    Iain, thats with an i by the way!

  3. #3
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    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
    Iain, thats with an i by the way!

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width