Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Dynamically Add textboxes and read values

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
   Nex
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

    Next
   
End Sub



Thanks,

jmsloan
0
jmsloan
Asked:
jmsloan
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
    Next
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)
    Next
End Sub
0
 
peetmCommented:
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
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now