Hmm I would approach this differently.
I would create class level variable that would contain a List Of TextBoxes. When the button was clicked I would create 4 new text boxes and add them to the FlowLayoutPanel1 and to the List Of TextBoxes.
Now, when I need to access the TextBoxes I would just loop through the TextBoxes in the List Of TextBoxes.....
So instead of having:
- Dim value1 As String = ""
-
Dim value2 As String = ""
-
Dim value3 As String = ""
-
Dim value4 As String = ""
-
Dim i As Integer
-
For i = 0 To FlowLayoutPanel1.Controls.Count - 1
-
Select Case FlowLayoutPanel1.Controls(i).Name
-
-
Case "TextBox1"
-
value1 = FlowLayoutPanel1.Controls(i).Text
-
-
Case "TextBox2"
-
value2 = FlowLayoutPanel1.Controls(i).Text
-
-
Case "TextBox3"
-
value3 = FlowLayoutPanel1.Controls(i).Text
-
-
Case "TextBox4"
-
value4 = FlowLayoutPanel1.Controls(i).Text
-
End Select
-
Next i
-
Dim strsql As String
-
strsql = "insert into stud values ('" & value1 & "','" & value2 & "','" & value3 & "','" & value4 & "')"
I would have:
-
'The following will add 4 more TextBoxes to the FlowLayoutPanel1 control
-
'and to the list of TextBoxes that holds a reference to all of the dynamically created TextBoxes.
-
Dim i As Integer
-
For i = 0 To 3
-
Dim aTextBox As New TextBox()
-
aTextBox.Name = "TextBox" + myListOfTextBoxes.Count.ToString
-
myListOfTextBoxes.Add(aTextBox)
-
FlowLayoutPanel1.Controls.Add(aTextBox)
-
Next i
-
Please note that myListOfTextBoxes is declared as a class level member and instantiated in the constructor for the class:
-
Private myListOfTextBoxes As List(Of TextBoxes)
-
-
Public Sub New()
-
' This call is required by the Windows Form Designer.
-
InitializeComponent()
-
myListOfTextBoxes = New List(Of TextBoxes)
-
End Sub
-
Later, when I need to access the dynamic TextBoxes I can just loop through the List Of TextBoxes:
-
Dim textBoxValues As New StringBuilder
-
For Each dynamicallyCreatedTextBox As TextBox In myListOfTextBoxes
-
textBoxValues.Append(dynamicallyCreatedTextBox.Text)
-
textBoxValues.Append(", ")
-
Next
-Frinny