Solved

How to dynamically create controls

Posted on 2004-10-08
10
146 Views
Last Modified: 2012-05-05
Please help:

I am trying to make a billing form.

I need to dynamically create lines of controlls.  Does anyone know how to do this?

I need to create a new line of controls once the previous line has been set, so that a person may specify an infinate number of items and other variables.

Thanks for the help.
0
Comment
Question by:antaren
10 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 168 total points
ID: 12258924
The best way to do this is place one of the controls you want to use on the form and set its Index property to 0.  This creats a control array.  To add other controls to the array at runtime, you call Load ControlName(i).

Here is an example using a TextBox (your form already has Text1(0) placed on it.

Dim i as Long

For i = 1 to 9
    Load Text1(i)
    Text1(i).Top = Text(0).Top + (Text(0).Height * i )
    Text1(i).Visible = True
Next

Hope it helps!
0
 
LVL 6

Assisted Solution

by:bkthompson2112
bkthompson2112 earned 166 total points
ID: 12258942
Hi antaren,

Here's another way using the Controls.Add method.

http://www.codeguru.com/vb/controls/vb_activex/activex/article.php/c3499/

bkt
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12259050
The advantage of using the control array is that you can easily receive the controls events.  e.g. in a dynamic TextBox control array you only need one Validate event for all the newly created controls in the array:

Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)
    'Use the Index property to determine which control fired the event
    Debug.Print "TextBox # " & Index & " Validate event"
 
    If Text1(Index).Text = "" Then
        MsgBox "Cannot be empty"
        Cancel = True
    End If
End Sub

 
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 12259051
>>I need to create a new line of controls once the previous line has been set, so that a person may specify an infinate number of items and other variables.


Have you think about to use listview to show data instead?
0
 

Author Comment

by:antaren
ID: 12263566
EDDYKT,
The list view would only be appropriate if it displays the selected items for the bill.  It may or may not be easier to set it up that way.  If I were using the list view, I would use one set of controls to select an item, and an ADD button that would place the selected item, quantity and price into the list box.

Erick37,
That is a nice trick, unfortunately the controls need to be created dynamically.  Even though your method would work, I would need to create each line of controls, which is what I'm trying to avoid in the first place.  The bill can have many items on it, and even if I created an arbitrary number (such as 50 lines) the user may still reach the limit.

bkthompson2112,
This method does look interesting, is there any way to use the controls.add method to create controls repeatedly on a list?  Can the controls be represented individually, specifically they each can contain a differant value?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12264004
Hi antaren:

You only need to create one line of controls during design time.  Each control in the line has its Index set to 0.  During runtime you would load lines 2 thru n.
0
 
LVL 10

Assisted Solution

by:anv
anv earned 166 total points
ID: 12265004
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now