Results 1 to 5 of 5

Thread: Creating multiple command buttons after runtime

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    44

    Creating multiple command buttons after runtime

    I was wondering how one would go about doing this? like be able to add all with a specific action that was all the same (yet different based on commandbutton # like cmd(num) where num is a integer) would do an action like msgbox variant(num)

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Creating multiple command buttons after runtime

    I'm sorry but when I read the subject of this thread I thought I understood what you wanted (and it's pretty easy to do) but after reading the text of your post I'm completely confused. Could you try again please?

  3. #3

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Creating multiple command buttons after runtime

    Here is a little hack. You need Form1, Command1 on it, and Class1. Paste as following:

    Code:
    ' Class1
    Option Explicit
    
    Dim m_CallType As VbCallType
    Dim m_ClickFunction As String
    Dim m_Parameters() As Variant
    Dim m_Parent As Object
    
    Public WithEvents Button As CommandButton
    
    Public Function Init(ByRef Parent As Form, ByRef ClickFunction As String, ByVal CallType As VbCallType, ParamArray Parameters()) As CommandButton
        If Not Parent Is Nothing Then
            m_CallType = CallType
            m_ClickFunction = ClickFunction
            m_Parameters = Parameters
            Set m_Parent = Parent
            Set Button = Parent.Controls.Add("VB.CommandButton", "Cmd" & Parent.Controls.Count)
            Set Init = Button
        End If
    End Function
    
    Private Sub Class_Terminate()
        Set Button = Nothing
    End Sub
    
    Private Sub Button_Click()
        CallByName m_Parent, m_ClickFunction, m_CallType, m_Parameters
    End Sub
    Code:
    ' Form1
    Option Explicit
    
    Dim Test As New Collection
    
    Public Sub ButtonClick(ByRef Parameters As Variant)
        MsgBox Parameters(0)
    End Sub
    
    Private Sub Command1_Click()
        Dim Reference As Class1
        Test.Add New Class1
        Set Reference = Test(Test.Count)
        With Reference.Init(Me, "ButtonClick", VbMethod, Test.Count)
            .Move 0, 360 * (Test.Count - 1), 3600, 360
            .Visible = True
        End With
    End Sub
    Booya, you can create as many command buttons as you like, you can set what parameters are passed to the click event on the form (there can be more than one!) and this enables you to have custom code that depends on what button was clicked.
    Last edited by Merri; Jan 14th, 2008 at 11:06 PM.

  5. #5
    Addicted Member jeanette_db's Avatar
    Join Date
    Oct 2005
    Location
    DC, Phil
    Posts
    215

    Re: Creating multiple command buttons after runtime

    just what i need....
    I learnt to plant my own garden instead of
    waiting for someone to bring me flowers
    visit my blog...

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