Solved

How to dynamically create controls

Posted on 2004-10-08
10
149 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code 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 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…
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…

821 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