Solved

Dynamic Control Arrays

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

738 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