Solved

Dynamic Control Arrays

Posted on 2000-05-04
5
178 Views
Last Modified: 2010-05-02
I am currently using the new VB6 function Controls() to dynamically create command buttons on a form.  However I am only able to pick up the event of the most recently created button.  Is there any way of being able to catch the events for each of the Command Buttons?  I use the following:

Dim WithEvents ctlCommand As VB.CommandButton

Private Sub Form_Click()
Set ctlCommand = Controls.Add("VB.CommandButton", "ctlCommand" & gintCommandCount, Form1)
End Sub

Private Sub ctlCommand_Click()
   
    MsgBox ctlCommand.Name
End Sub
0
Comment
Question by:justinhemphill
  • 3
5 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 2777749
Create the first one manually and then create it's event. It should have an Index property as a parameter in the _Click(index) to tell which of the controls fired.

M
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 150 total points
ID: 2777811
rather than use that method why not give the first one an index of 0 and use the Load method for each additional one.  

Load Command1(1)
Command1.Left =
Command1.Top =
Command1.Visible = True

This ensures that a command click event will include an index parameter
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2777871
You need to delcare a variable to hold the reference to the control. Use the add method to create the instance of the control. Set the properties of the control to position it on the form and make it visible to you.

Try something like this...

Private Sub Form_Click()
Dim ocmdnew As CommandButton

Set otxtnew = Controls.Add("VB.CommandButton", "ocmdNew")
With ocmdnew
..Visible = True
..Caption = "New Control"
..Width = Me.TextWidth(.Text) + 200
..Height = Me.TextHeight(.Text) + 50
..Top = 100
..Left = 300
End With
End Sub
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2777888
Oops... Few errors... This is the correct.

Private Sub Form_Click()
Dim ocmdnew As CommandButton
Set ocmdnew = Controls.Add("VB.CommandButton", "ocmdNew")
With ocmdnew
..Visible = True
..Caption = "New Control"
..Top = 100
..Left = 300
End With
End Sub
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2777902
You will get the message when you click on the new button.

Option Explicit
Dim WithEvents ocmdnew As CommandButton

Private Sub Form_Click()

Set ocmdnew = Controls.Add("VB.CommandButton", "ocmdNew")
With ocmdnew

..Visible = True
..Caption = "New Control"
..Top = 100
..Left = 300
End With
End Sub

Private Sub ocmdnew_Click()
    MsgBox ocmdnew.Name
End Sub
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question