Question Generate Text boxes programmatically using generated text box names

wilson208

Member
Joined
Jun 21, 2011
Messages
9
Programming Experience
Beginner
Hi, there is probably a really simple answer to this but i cant find one anywhere!

i am trying to automatically generate multiple text boxes on a form with the following code
VB.NET:
 Private Sub CreateTB(ByVal x As Integer)

        Dim y As Integer = 1

        Do Until y = x
            Dim newtxtbox As TextBox

            'Here will contain property settings for new text box when i work them out

            y = y+1
        Loop


    End Sub

i need it to generate as many text boxes as the variable x states, so i used the do until loop thing. But i am stuck when it comes to naming the text boxes because obviously all the text boxes cannot share the same name. so i would like to know how to programmatically name each textbox uniquely.
Many Thanks, Wilson
 

wilson208

Member
Joined
Jun 21, 2011
Messages
9
Programming Experience
Beginner
i thought that the text boxes would need to be named uniquely so that i can use what is in them later. sorry i havent done much of this before.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,744
Location
Sydney, Australia
Programming Experience
10+
The Name of a control is used as a key in its Parent's Controls collection, so if you want to be able to get the control from that collection by name then it must have a name. The thing is, the best you'd be able to do to distinguish one from another is use a numeric suffix on the names, so that makes them pointless anyway.

Just add them to a collection and then access them by index if you need to do so. Most likely you wouldn't need to anyway.

You could even add a TableLayoutPanel or FlowLayoutPanel to your form and add the controls directly to that, so it can handle the layout for you. The panel's Controls property can then be your collection so you don't need to create one explicitly.
 

Flippedbeyond

Well-known member
Joined
Dec 19, 2008
Messages
198
Programming Experience
1-3
Top Bottom