Solved

How to create objects dynamically with unknow number of them

Posted on 2004-10-13
6
228 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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