Solved

How to create objects dynamically with unknow number of them

Posted on 2004-10-13
6
227 Views
Last Modified: 2012-05-05
I have an example of how to create a combo box dynamically, but I want to create multiple of it with the number of it from an input on run time.

Here an example of creating a combo box on run time:

Dim WithEvents cboControl As ComboBox
Set cboControl = Controls.Add("VB.ComboBox", "cboControl")

but how to create more than one? Please give code.
0
Comment
Question by:limct
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12297081
It would probably be easier to use a control array:

Place a combobox on your form
set its index to 0 and Visible = False

When you need to add a new one:

        lIndex = cboControl.Ubound
        Load cboControl(LIndex)
        With cboControl(LlngIndex)
            .Container = ...
            .ToolTipText = ...
            .TabStop = True
            .TabIndex =..
            .Left = ...
            .Top = ...
            .Visible = True
        End With



0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12297108
Oops...
        Load cboControl(cboControl.Ubound + 1)
        With cboControl(cboControl.Ubound)
            .Container = ...
            .ToolTipText = ...
            .TabStop = True
            .TabIndex =..
            .Left = ...
            .Top = ...
            .Visible = True
        End With

Fill in ...s with your stuff
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12297138
I would certainly be better to use an array. But if you still don't want to use one, here's what you can do...

------------------------------------------------------------------------------------------------------
Dim WithEvents cboControl As ComboBox
Dim Num as Integer
Dim I as Integer

Num = InputBox("How many combo boxes to add","How Many")

For I = 1 To Num
     Set cboControl = Controls.Add("VB.ComboBox", "cboControl" & Num)
Next I
------------------------------------------------------------------------------------------------------

Hence you'll have new controls as cboControl1, cboControl2, cboControl3, cboControl4, etc...

But once again, it'll be better to use Arrays since the main control name will be constant and it provides many other advantages. Hope this answers your question :)
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 500 total points
ID: 12297182
Try looking at http://vbcity.com/forums/topic.asp?tid=4953

There was someone there trying to do the same thing with text boxes.  It appears you cannot create a control array at runtime but a poster there suggested that you can wrap them in a class...

HTH

J.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12297230
eeshahidt:

Yes, but in your example the events for all but the last added control will be lost. The WithEvents clause only works for cboControl. In this case cboControl refer to the last control added in the For loop.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12297472
Oh! Well glad you got your answer. I learnt a little as well ;)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 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…

816 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

7 Experts available now in Live!

Get 1:1 Help Now