-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
Replies: 3
Last Post: 01-12-2009, 08:20 AM
-
By kamranabbas14 in forum VB Classic
Replies: 1
Last Post: 11-19-2008, 07:25 AM
-
By rusharif in forum VB Classic
Replies: 2
Last Post: 05-27-2008, 03:37 PM
-
By Shravan in forum VB Classic
Replies: 5
Last Post: 05-24-2002, 05:10 AM
-
By cassandra in forum Architecture and Design
Replies: 0
Last Post: 12-01-2000, 04:01 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks