Hi dnnyobi,
Check this example. Now please take its reference and correct your code.
Form Design
Form contains two Buttons, one ListBox, one Panel and 4 TextBoxes inside the panel.
Code
C#
private void btnAdd_Click(object sender, EventArgs e)
{
foreach (TextBox item in pnlFruits.Controls.OfType<TextBox>())
{
lstFruits.Items.Add(item.Text);
item.Text = "";
}
}
private void btnRetrieve_Click(object sender, EventArgs e)
{
int i = 0;
foreach (string item in lstFruits.Items)
{
List<TextBox> textBoxes = pnlFruits.Controls.OfType<TextBox>().ToList();
textBoxes[i].Text = item;
i++;
}
lstFruits.Items.Clear();
}
VB.Net
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
For Each item As TextBox In pnlFruits.Controls.OfType(Of TextBox)()
lstFruits.Items.Add(item.Text)
item.Text = ""
Next
End Sub
Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click
Dim i As Integer = 0
For Each item As String In lstFruits.Items
Dim textBoxes As List(Of TextBox) = pnlFruits.Controls.OfType(Of TextBox)().ToList()
textBoxes(i).Text = item
i += 1
Next
lstFruits.Items.Clear()
End Sub
Screenshot