Solved

How to dynamically create controls

Posted on 2004-10-08
10
152 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

632 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