Solved

Dynamic Control Arrays

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 60
passing parameter in sql procedure 9 63
Using "ScreenUpdating" 6 65
passing a value with stream reader AFTER a ";" 3 67
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…

810 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