I've tried following the Microsoft instructions to add multiple Textboxes to my form, but just cannot get it to work.
Thus, I've deleted everything I wrote, so I don't have code to post. Is there a very simple way to add multiple
TextBoxes to a form and be able to index them? If it's too lengthy, please let me know. Is there a workaround?

Thanks
major_lost

There is a simple way to add multiple textboxes to a form at runtime and store references to them in an array so that they can be accessed by index. I created an example a few months ago to demonstrate this technique using buttons but you can easily use any other type of control. The technique basically boils down to

1) create an array of the given control to hold the references
2) code a loop to create the controls in which you
3) create a new control
4) set the control properties (location, size, etc)
5) assign a handler to handle events for that control
6) add the control to Me.Controls
7) assign the control to your array

I've attached the RunTimeButtons project. The constant SQSIZE defines the square size of the button array. SQSIZE = 5 will generate a 5x5 grid of buttons. For more options on laying out dynamic controls you can use the TableLayoutPanel control.

Reverend Jim;
I cannot open your example because I'm using VB.net 2003 (ver 1.1). Thus I don't get what your trying to explain. Sorry. major_lost

You could still unzip it and look at the vb files. In any case here is a simple example I just coded up for you. I hope this helps. Please remember to mark this thread as solved when yoou get what you need. Feel free to ask any follow up questions.

'         
'  Name:  
'         
'    DynamicTextBoxes  
'                      
'  Description:        
'                      
'    Simple example of how to create and use textboxes at run time  
'        
'  Audit
'        
'    2012-05-19  rj 
'                   

Public Class Form1

    Private boxes(5) As TextBox

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim newbox As TextBox

        For i As Integer = 1 To 5

            'create a new textbox and set its properties

            newbox = New TextBox
            newbox.Size = New Drawing.Size(100, 20)
            newbox.Location = New Point(10, 10 + 25 * (i - 1))
            newbox.Name = "TextBox" & i
            newbox.Text = newbox.Name

            'connect it to a handler, save a reference to the array and add it to the form controls

            AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
            boxes(i) = newbox
            Me.Controls.Add(newbox)

        Next

    End Sub

    Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs)

        'when you modify the contents of any textbox, the name of that textbox and
        'its current contents will be displayed in the title bar

        Dim box As TextBox = DirectCast(sender, TextBox)
        Me.Text = box.Name & ": " & box.Text

    End Sub

End Class

Fantastic!!! Works like a champ and shorter than MS's 9 page solution!!! Now all I need to figure out is how to get these textboxes organized in a 5 x 5 matrix (YEP 25 of them!) I already know which two lines to change to get 25, just not sure how to change line 29. THANK YOU for your help.

Reverend Jim;

This is what I've been able to accomplish. Again, thank you for your help.

ml

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim txtBox As TextBox
        Dim x As Integer = 1


        For i As Integer = 1 To 25

            txtBox = New TextBox
            txtBox.Size = New Drawing.Size(100, 20)

            Select Case i

                Case 1 To 5
                    txtBox.Location = New Point(10, 10 + 25 * (x - 1))
                    If i = 5 Then x = 0

                Case 6 To 10
                    txtBox.Location = New Point(120, 10 + 25 * (x - 1))
                    If i = 10 Then x = 0

                Case 11 To 15
                    txtBox.Location = New Point(230, 10 + 25 * (x - 1))
                    If i = 15 Then x = 0

                Case 16 To 20
                    txtBox.Location = New Point(340, 10 + 25 * (x - 1))
                    If i = 20 Then x = 0

                Case 21 To 25
                    txtBox.Location = New Point(450, 10 + 25 * (x - 1))

            End Select

            txtBox.Name = "TextBox" & i
            txtBox.Text = txtBox.Name

            AddHandler txtBox.TextChanged, AddressOf TextBox_TextChanged
            boxes(i) = txtBox
            Me.Controls.Add(txtBox)
            x = x + 1

        Next
    End Sub

A little cleaner solution would be as follows

'                                                                                               
'  Name:                                                                                        
'                                                                                               
'    DynamicTextBoxes                                                                           
'                                                                                               
'  Description:                                                                                 
'                                                                                               
'    Simple example of how to create and use textboxes at run time                              
'                                                                                               
'  Audit:                                                                                       
'                                                                                               
'    2012-05-19  rj                                                                             
'                                                                                               

Public Class Form1

    Private boxes(4, 4) As TextBox

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim newbox As TextBox

        For row As Integer = 0 To 4
            For col As Integer = 0 To 4

                'create a new textbox and set its properties

                newbox = New TextBox
                newbox.Size = New Drawing.Size(100, 20)
                newbox.Location = New Point(10 + 100 * row, 10 + 25 * col)
                newbox.Name = "TextBox" & row & "_" & col
                newbox.Text = newbox.Name

                'connect it to a handler, save a reference to the array and add it to the form controls

                AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
                boxes(row, col) = newbox
                Me.Controls.Add(newbox)

            Next
        Next

    End Sub

    Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs)

        'when you modify the contents of any textbox, the name of that textbox and
        'its current contents will be displayed in the title bar

        Dim box As TextBox = DirectCast(sender, TextBox)
        Me.Text = box.Name & ": " & box.Text

    End Sub

End Class

it did not work

I just created a new VB 2010 project, copied the above code into it then ran it. It works as stated.

sorry i omitted the last part the Textbox_TextChanged event code.i just added it now and it worked as stated.

I'm attempting to do similar things here, only with multiple labels, buttons and textboxes. I've attempted to insert the needed code to display these controls on the form. Unfortunately with I debug it, the form is still blank. Here is what I've written:

Public Sub frmOrderEntry2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load

    '
    'This creates the label "Part No."
    Dim lblPrtNo As Label = New Label
    lblPrtNo.Name = "lblPrtNo"
    lblPrtNo.Text = "Part Number"
    lblPrtNo.ForeColor = Color.White
    lblPrtNo.Font = New System.Drawing.Font("Sans Serif", 9)
    lblPrtNo.Font = New System.Drawing.Font(lblPrtNo.Font, FontStyle.Regular)
    lblPrtNo.Location = New Point(6, 8)
    lblPrtNo.Size = New Size(77, 15)
    lblPrtNo.TextAlign = ContentAlignment.MiddleLeft
    lblPrtNo.Enabled = False
    AddHandler lblPrtNo.TextChanged, AddressOf lblPrtNo_Textchanged
    Me.Controls.Add(lblPrtNo)
    '
    'This generates the textbox for the user to enter the Part No.
    Dim txbPartNo As TextBox = New TextBox
    txbPartNo.Name = "txbPartNo"
    txbPartNo.Text = ""
    txbPartNo.ForeColor = Color.Black
    txbPartNo.BackColor = Color.White
    txbPartNo.Font = New System.Drawing.Font("Sans Serif", 10)
    txbPartNo.Font = New System.Drawing.Font(txbPartNo.Font, FontStyle.Bold)
    txbPartNo.Location = New Point(6, 26)
    txbPartNo.Size = New Size(263, 22)
    txbPartNo.TextAlign = ContentAlignment.MiddleLeft
    txbPartNo.Cursor = Cursors.Hand
    txbPartNo.AcceptsReturn = True
    txbPartNo.AcceptsTab = True
    txbPartNo.TabIndex = 10
    txbPartNo.Enabled = True
    AddHandler txbPartNo.TextChanged, AddressOf txbPartNo_Textchanged
    Me.Controls.Add(txbPartNo)

     Private Sub lblPrtNo_Textchanged(ByVal sender As System.Object, ByVal e As EventArgs)
    Dim lblPrtNo As Label = DirectCast(sender, Label)
    Me.Text = lblPrtNo.Name & ": " & lblPrtNo.Text
    lblPrtNo.Show()

End Sub

Private Sub txbPartNo_Textchanged(ByVal sender As System.Object, ByVal e As EventArgs)
    Dim txbPartNo As TextBox = DirectCast(sender, TextBox)
    Me.Text = txbPartNo.Name & ": " & txbPartNo.Text
    txbPartNo.Show()
End Sub

Again, please be aware that this is just 1 label and 1 textbox that of many that will be on the form.

Am I missing something? Do you see where my error is at.

In advance, thanks for your help!

@doncwilson_1 this is a solved question, you're supposed to start you're own, rather than piggyback someone elses. As far as I know it's ok to referencxe this question in a link if you need to.

how do I get the value of the dynamically created textbox and save it to Database?

You could access it by name if a name was specified when the control was created or you could access it by address if a reference to the control was saved when the control was created. Without you being more specific it is hard to give a specific answer. As for the second part of the question - same response.

If you had read the Daniweb Daniweb Posting Rules you would have started a new thread with your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.