Solved

Dynamic Control Arrays

Posted on 2000-05-04
5
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

717 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