DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2004
    Posts
    94

    How to create a dynamic control array with 100 commandbuttons?

    Hi, I want to make a new dynamic control array with 100 commandbuttons.But I don't know how to do it?thanks a lot.
    xuliangchu

  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    put a button in your form, and set its Index property to 0
    Then in the form load:

    Code:
        Dim k As Long
        For k = 1 To 99
            Load myButton(k)
            With myButton(k)
                '' set here position, caption and other properties
                .Visible = True
            End With
        Next
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    Or
    Code:
    Option Explicit
    
    Private WithEvents myButton As VB.CommandButton
    
    Private Sub Command1_Click()
    Dim k As Long
        For k = 1 To 99
            Set myButton = Controls.Add("VB.CommandButton", _
                        "myButton" & k, Form1)
            With myButton
              'set here position, caption and other properties
                .Visible = True
            End With
        Next
    End Sub
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Hack, that code will fire the events only for the last command button added to the form, that is the last time the myButton variable has been assigned.

    Getting events from an array of dynamically added controls (like in your code) is possible, but is also e royal pain (for example using a collection of classes, one of each control that was added)

    This is why I suggested to use the Load method (that, unlike yours, does create an 'array' :-) ) I do indeed us the Controls.Add method, but only for my own controls, because I that case I need only an event sync class, but I am afraid will go off topic. This is a very interesting issue in VB6, that fortunately has been fixed in .net
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    I ran into problems using your example (although I can't remember what now)
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #6
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Open VB, create a new exe project and cut and paste the code below
    Add a button in the Form, call it "myButton" and set its Index property to 0 (carefull, not the TabIndex property...), move the button at position 0,0
    Run the project and click on any button to see the event
    Let me know if you get any trouble.
    Have fun!

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim k As Long
        For k = 1 To 99
            Load myButton(k)
            With myButton(k)
                .Left = k * 120
                .Top = k * 120
                .Visible = True
            End With
        Next
    End Sub
    
    Private Sub myButton_Click(Index As Integer)
        Debug.Print "Button " & Index
    End Sub
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  7. #7
    Join Date
    Oct 2005
    Location
    Staffordshire, England
    Posts
    101
    100 command buttons what a confusing app?
    I've been programming with VB for 15 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

    Martin2k

  8. #8
    Join Date
    Jun 2012
    Posts
    1
    This works great for me. Thanks. If we dynamically create object using me.control.add click event is working only for the last control. but the following code works good to handle events. Once again thanks to mstraf

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim k As Long
        For k = 1 To 99
            Load myButton(k)
            With myButton(k)
                .Left = k * 120
                .Top = k * 120
                .Visible = True
            End With
        Next
    End Sub
    
    Private Sub myButton_Click(Index As Integer)
        Debug.Print "Button " & Index
    End Sub
    [/QUOTE]

Similar Threads

  1. Control Array
    By shers in forum .NET
    Replies: 3
    Last Post: 01-12-2009, 08:20 AM
  2. Replies: 1
    Last Post: 11-19-2008, 07:25 AM
  3. Check Box Control Array
    By rusharif in forum VB Classic
    Replies: 2
    Last Post: 05-27-2008, 03:37 PM
  4. Replies: 5
    Last Post: 05-24-2002, 05:10 AM
  5. Create a Dynamic Web page using Active X control
    By cassandra in forum Architecture and Design
    Replies: 0
    Last Post: 12-01-2000, 04:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links