Solved

How to create objects dynamically with unknow number of them

Posted on 2004-10-13
6
225 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Oh! Well glad you got your answer. I learnt a little as well ;)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

772 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

9 Experts available now in Live!

Get 1:1 Help Now