Solved

Dynamic Control Arrays

Posted on 2000-05-04
5
175 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now