In this article I will explain with an example, how to dynamically add and remove TextBox in Windows Forms (WinForms) Application using C# and VB.Net.
The TextBox and its associated Delete Button will be dynamically created on Button click. When the Delete Button for a TextBox is clicked, the TextBox and the Button will be removed.
 
 
Form Design
The Form consists of a Button and a Panel control.
Dynamically Add Remove TextBox in Windows Forms Application using C# and VB.Net
 
 
Dynamically adding TextBox in Windows Forms Application
When the Add Button is clicked, the TextBox and the Button control is dynamically created and added to the Panel control.
C#
private void btnAdd_Click(object sender, EventArgs e)
{
    //Create the dynamic TextBox.
    TextBox textbox = new TextBox();
    int count = panel1.Controls.OfType<TextBox>().ToList().Count;
    textbox.Location = new System.Drawing.Point(10, 25 * count);
    textbox.Size = new System.Drawing.Size(80, 20);
    textbox.Name = "txt_" + (count + 1);
    panel1.Controls.Add(textbox);
 
    //Create the dynamic Button to remove the TextBox.
    Button button = new Button();
    button.Location = new System.Drawing.Point(95, 25 * count);
    button.Size = new System.Drawing.Size(60, 20);
    button.Name = "btnDelete_" + (count + 1);
    button.Text = "Delete";
    button.Click += new System.EventHandler(this.btnDelete_Click);
    panel1.Controls.Add(button);
}
 
VB.Net
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
    'Create the dynamic TextBox.
    Dim textbox As TextBox = New TextBox
    Dim count As Integer = panel1.Controls.OfType(Of TextBox).ToList.Count
    textbox.Location = New System.Drawing.Point(10, (25 * count))
    textbox.Size = New System.Drawing.Size(80, 20)
    textbox.Name = "txt_" & (count + 1)
    panel1.Controls.Add(textbox)
    'Create the dynamic Button to remove the TextBox.
    Dim button As Button = New Button
    button.Location = New System.Drawing.Point(95, (25 * count))
    button.Size = New System.Drawing.Size(60, 20)
    button.Name = "btnDelete_" & (count + 1)
    button.Text = "Delete"
    AddHandler button.Click, AddressOf Me.btnDelete_Click
    panel1.Controls.Add(button)
End Sub
 
 
Dynamically removing TextBox in Windows Forms Application
When the Delete Button is clicked, the first the Button which was clicked is referenced and its Index is determined.
Then using the Index, the TextBox is referenced from the Panel control and both the TextBox and Button are removed.
Finally, the location of other TextBoxes and Buttons are arranged to fill the gap created.
C#
private void btnDelete_Click(object sender, EventArgs e)
{
    //Reference the Button which was clicked.
    Button button = (sender as Button);
 
    //Determine the Index of the Button.
    int index = int.Parse(button.Name.Split('_')[1]);
 
    //Find the TextBox using Index and remove it.
    panel1.Controls.Remove(panel1.Controls.Find("txt_" + index, true)[0]);
 
    //Remove the Button.
    panel1.Controls.Remove(button);
 
    //Rearranging the Location controls.
    foreach (Button btn in panel1.Controls.OfType<Button>())
    {
        int controlIndex = int.Parse(btn.Name.Split('_')[1]);
        if (controlIndex > index)
        {
            TextBox txt = (TextBox)panel1.Controls.Find("txt_" + controlIndex, true)[0];
            btn.Top = btn.Top - 25;
            txt.Top = txt.Top - 25;
        }
    }
}
 
VB.Net
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Reference the Button which was clicked.
    Dim button As Button = CType(sender, Button)
 
    'Determine the Index of the Button.
    Dim index As Integer = Integer.Parse(button.Name.Split("_")(1))
 
    'Find the TextBox using Index and remove it.
    panel1.Controls.Remove(panel1.Controls.Find(("txt_" & index), True)(0))
 
    'Remove the Button.
    panel1.Controls.Remove(button)
 
    'Rearranging the Location controls.
    For Each btn As Button In panel1.Controls.OfType(Of Button)()
       Dim controlIndex As Integer = Integer.Parse(btn.Name.Split("_")(1))
        If (controlIndex > index) Then
            Dim txt As TextBox = CType(panel1.Controls.Find(("txt_" & controlIndex), True)(0), TextBox)
            btn.Top = (btn.Top - 25)
            txt.Top = (txt.Top - 25)
        End If
    Next
End Sub
 
 
Screenshot
Dynamically Add Remove TextBox in Windows Forms Application using C# and VB.Net
 
 
Downloads