Results 1 to 9 of 9

Thread: Adding command buttons at Runtime

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    14

    Question

    I am trying to write a database driven application which will have a different number of command buttons (from 0 - 20) depending on the result of database query. How can I add command buttons to a form at runtime?

  2. #2
    Guest
    Hi,

    Put one CommandButton on your Form.
    Give it the Index 0.
    Load as many Buttons you like with
    Code:
    Load Command1(x)
    Note: the loaded controls are initially invisible.

    Regards
    da_bob
    _______________


  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    Andrew, if you are using VB5, the first reply answers your query. If you have VB6, however, you can add a control at runtime:

    Code:
    'Declarations section
    Dim WithEvents newButton As CommandButton
    
    Private Sub Form_Activate()
        Set newButton = Me.Controls.Add("VB.CommandButton", _
                        "Button1")
    End Sub
    Check out the MSDN help for adding controls dynamically. VB.CommandButton is the name of the class you are using, while Button1 will be the name of the command button.

    Please reply if you have any doubts on this subject.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    Guest
    me again

    I don't know anything about VB5 since I'm a programmer for 1.5 years only.
    The disadvantage with your solution honeybee is that you don't have a benefit of adding the Buttons dynamically because
    you need to declare the maximum number of Buttons you want to use in order to sink their events- this is not very dynamical.

    Regards,
    da_bob
    ____________

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'as per msdn  dynamically added command button
    
    Option Explicit
    
    Private WithEvents btnObj As CommandButton
    
    Private Sub btnObj_Click()
       MsgBox "This is a dynamically added button."
    End Sub
    
    Private Sub Form_Load()
       Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
       With btnObj
          .Visible = True
          .Width = 2000
          .Caption = "Hello"
          .Top = 1000
          .Left = 1000
       End With
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Guest
    Here is a short and simple method.

    Code:
    Controls.Add "VB.CommandButton", "Button1"
    Me!Button1.Move 0, 0
    Me!Button1.Caption = "New Button"
    Me!Button1.Visible = True

  7. #7
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    Exactly, HeSaidJoe!

    da_bob,
    Remember, you can dynamically add controls, but not events. What we actually have to do is when we declare a CommandButton object with
    Code:
    Dim Withevents newCommandButton as CommandButton
    we also have to declare any events that should be associated with the command buttons that we are going to add dynamically. It's similar to the method you use with control arrays. So you are wrong when you say that we need to declare the max. no. of buttons to add to sink in the events. There is only one event.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8
    Guest
    Sounds cool .
    Could you give me a complete code-sample too?
    I never managed to add a control array dynamically with the Controls.Add function.

    Regards
    da_bob
    ____________

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    da_bob, sorry for the late reply. But you cannot add a control array by adding elements at runtime. See the thread on Setting Index Property of dynamically added controls in the General VB section for more details.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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