Solved

How to dynamically create controls

Posted on 2004-10-08
10
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

739 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