Dynamically Add textboxes and read values

Posted on 2007-10-11
Last Modified: 2010-04-30
I am using VB6

I have a button called Command1 and a label called Label1.  Onload I would like to  to dynamically add text boxes to the form called Form1.  For now lets say 5 texboxes.  When the user clicks the button. I would like to have the label say textbox1 is #value1, textbox2 is #value2,  textbox3 is #value3,  textbox4 is #value4,  textbox5 is #value5.

Here is the code I have to create the textboxes, but how do I reference the textboxes names?  
Is the second argument of Controls.Add textbox name?  It there an easier way to do this? With the current code the label on displays the values of the last text box.

Option Explicit
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox

Private Sub Command1_Click()
   Dim i As Integer
   For i = 0 To 5
       Label1.Caption = Label1.Caption & ctlText.Text
End Sub

Private Sub Form_Load()
    Dim i As Integer
    Dim height As Integer

    Licenses.Add "MSComctlLib.TreeCtrl"
    Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", "myctl", Form1)
    ' set the location and size of the control.
    ctlDynamic.Move 1, 1, 2500, 3500
    height = 0
    For i = 0 To 5
                height = height + 350
            'Add text box
            Set ctlText = Controls.Add("VB.TextBox", "ctlText" & i, Form1)
            ' Set the location and size of the textbox
            ctlText.Move (100), height, 975, 250
            ctlText.Visible = True
            ctlText.TabIndex = i + 1

End Sub


Question by:jmsloan
    LVL 69

    Expert Comment

    by:Éric Moreau
    LVL 85

    Accepted Solution

    VB6 can't do it this way as "ctlText" will only ever refer to the last control created.  For this type of functionality move on up to VB.Net!

    In VB6, you need to use a CONTROL ARRAY:
    Create a new Project.
    Add a CommandButton, a Label, and a TextBox.
    Set the Index property of Text1 to 0 (zero).

    Option Explicit

    Private Sub Form_Load()
        Dim i As Integer
        For i = 1 To 5
            Load Text1(i)
            Text1(i).Top = Text1(Text1.LBound).Top + (Text1(Text1.LBound).Height * i)
            Text1(i).Visible = True
    End Sub

    Private Sub Command1_Click()
        Dim i As Integer
        Label1.Caption = Text1(Text1.LBound).Text
        For i = Text1.LBound + 1 To Text1.UBound
            Label1.Caption = Label1.Caption & ", " & Text1(i)
    End Sub
    LVL 10

    Expert Comment

    I think I'd be more inclined to use a control array:

        Dim i As Integer
        For i = 1 To 5
            Call Load(Text1(i))
            With Text1(i)
              .Text = i
              .Visible = True
              .Top = Text1(i - 1).Top + 100
            End With
        Next i

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    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…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    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…

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now