Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code I have listed creates a list of items and adds every item in a single listbox named ListBox1.

Is there a way I can make a for loop to put each item in a textbox? Each times it adds an item, it will store it by automatically increment a textbox's number.

For example, if the list contains 5 items, the program will put the first item in a textbox named Textbox1, the next item in a textbox named Textbox2, the next item in a textbox named Textbox3, and so on.

Can anyone show me how to create a new textbox inside a loop in order for each item to be extracted to each textbox?

What I have tried:

For Each item As Match In (New Regex(price)).Matches(source)

            ListBox1.Items.Add(item.Value)

        Next
Posted
Updated 8-Apr-20 20:15pm
v2

1 solution

Just create a new textbox inside a loop:

VB.NET
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Form1

    Dim myTxtBoxes As List(Of TextBox) = New List(Of TextBox)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Input string.
        Dim sText As String = Me.TextBox1.Text
        Dim sPattern As String = Me.TextBox2.Text
        Dim initialPoint As Point = New Point(Me.TextBox2.Location.X, Me.TextBox2.Location.Y)

        ' Call Regex.Matches method.
        Dim matches As MatchCollection = Regex.Matches(sText, sPattern)

        'clear previous result
        For j As Integer = myTxtBoxes.Count - 1 To 0 Step -1
            Dim tb As TextBox = myTxtBoxes(j)
            Me.Controls.Remove(tb)
            myTxtBoxes.Remove(tb)
        Next

        ' Loop over matches and add results into textboxes
        Dim i As Integer = 1
        For Each m As Match In matches
            Dim tb As TextBox = New TextBox
            With tb
                .Name = "ResultTB" & i.ToString()
                .Location = New Point(initialPoint.X, initialPoint.Y + (i * 24) + 8)
                .Size = Me.TextBox2.Size
                .Text = m.Value
                .BackColor = Color.Aquamarine
                .Parent = Me
            End With
            myTxtBoxes.Add(tb)
            i += 1
        Next
    End Sub
End Class


[EDIT]
Download working example: DynamicControls_vb.7z[^] (available to download between 7AM and 10:59PM CET +1)
 
Share this answer
 
v3
Comments
Member 14789896 9-Apr-20 2:12am    
@losmac Thank you for your comment. I am a beginner in visual basic and I am trying to integrate your textbox loop with the code I have but I am unable to. Can you please help me? Below there is a matches function code that would output the string "said shed spread" in a listbox. May you integrate your code in this one so it would output "said" in Textbox1, "shed" in Textbox2 and "spread" in Textbox3?



' Input string.
Dim value As String = "said shed spread"

' Call Regex.Matches method.
Dim matches As MatchCollection = Regex.Matches(value, "s\w+d")

' Loop over matches.
For Each m As Match In matches
ListBox1.Items.Add(m.Value)
Next
Maciej Los 9-Apr-20 2:54am    
Have you read my answer? There's a direct instruction: "Just create a new textbox inside a loop". What part of my answer is not clear?
"Inside a loop" means between For ... Next.
Ralf Meier 9-Apr-20 6:56am    
to complete the answer of Maciej :
- Location is a point inside your Form where the Textbox should appear - this point has to be calculated by you
- SomeNumber is the Number of the Textbox - inside YOUR Loop this Number must be incremented by you

If your code really doesn't work : improve your question with the new Code - completed by the Suggestion of Maciej ...
Maciej Los 9-Apr-20 7:03am    
Thank you, Ralf.
Member 14789896 9-Apr-20 9:13am    
Thank you for both of you to take the help me but again, I am having problems integrating your code. Right now, I cannot seem to increment the textboxes. This is what I tried but instead of outputting three textboxes, it outputs a single textbox in the default location:


' Input string.
Dim value As String = "said shed spread"

' Call Regex.Matches method.
Dim matches As MatchCollection = Regex.Matches(value, "s\w+d")

' Loop over matches.
For Each m As Match In matches

Dim tb As TextBox = New TextBox()

With tb
Dim i As Integer
For i = 1 To 3
.Name = String.Concat("TextBox", i.ToString())
.Parent = Me 'this command places textbox on the form!
'other properties

Next

End With

Next



Clearly, I am not writing the code the right way. And if I can get it to create three textbox, I still do not know how to make each item store the values in the textboxex; unless its in a listbox like my code in the question.

What I am hoping to output is to have three textboxes, with the string "said" in Textbox1, "shed" in Textbox2 and "spread" in Textbox3. Again, I am a beginner in visual basic and I am asking for help to learn

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900