Dynamically add and Delete Control in Windows Form C# .Net

Last Reply on Jan 15, 2016 05:50 AM By Shashikant

Posted on Jan 14, 2016 09:56 AM

 

Hello,
I wrote the following code to create buttons textboxes dynamically 

Now I would like to add delete functionality to each button that dynamically created.
The button should delete the textbox and button itself when it clicked and also automatically updated by the next textbox and button.

Can anyone provide a sample code for it?

Thank you 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Web;



namespace UIApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int A = 1
        int B = 1;
        //int C = 1;
        private void button1_Click(object sender, EventArgs e)
        {            
            //AddNewLabel();
            AddNewTextBox();
            AddNewButton();

            
        }
                public System.Windows.Forms.TextBox AddNewTextBox()
        {
            System.Windows.Forms.TextBox text = new System.Windows.Forms.TextBox();
            this.Controls.Add(text); 
            text.Top = B * 35;
            text.Left = 150;
            text.Text = "textbox " + this.B.ToString();
            B = B + 1;
            return text;
        }
        List<Button> buttonsAdded = new List<Button>();

        public System.Windows.Forms.Button AddNewButton()
        {
            System.Windows.Forms.Button button = new System.Windows.Forms.Button();
            this.Controls.Add(button ";
          
            button.Top = A * 35;
            button.Left = 255;
            button.Text = " Delete"; 
            A = A + 1;
            buttonsAdded.Insert(0, button);
            
            //to add delete functionality to dynamically created button

            //if (buttonsAdded.Count > 0)
            //{
            //    Button buttonToRemove = buttonsAdded[0];
            //    buttonsAdded.Remove(buttonToRemove);
            //    this.Controls.Remove(buttonToRemove);
            //}
            

            return button;
        }
    }
}

 

Posted on Jan 15, 2016 05:50 AM

Hi,

Please refer below code.

C#

int A = 0;
int B = 0;
public System.Windows.Forms.TextBox AddNewTextBox()
{
    System.Windows.Forms.TextBox text = new System.Windows.Forms.TextBox();
    text.Name = "txt" + B;
    this.Controls.Add(text);
    textAdded.Insert(B, text);
    text.Top = B * 35;
    text.Left = 150;
    B = B + 1;
    return text;
}
List<TextBox> textAdded = new List<TextBox>();

public System.Windows.Forms.Button AddNewButton()
{
    System.Windows.Forms.Button button = new System.Windows.Forms.Button();
    button.Name = "btn" + A;
    button.Click += new EventHandler(DeleteButton);
    this.Controls.Add(button);
    button.Top = A * 35;
    button.Left = 255;
    button.Text = " Delete";
    A = A + 1;
    return button;
}

private void button1_Click(object sender, EventArgs e)
{
    AddNewButton();
    AddNewTextBox();
}

private void DeleteButton(object sender, EventArgs e)
{
    Button btn = (sender as Button);
    string name = btn.Name;
    name = name.Replace("btn", "");
    Control[] cntrls = this.Controls.Find("txt" + name, false);
    TextBox txt = cntrls[0] as TextBox;
    if (this.Controls.Contains(txt))
    {
        this.Controls.Remove(txt);
        this.Controls.Remove(btn);
    }
}

VB

Private A As Integer = 0
Private B As Integer = 0
Public Function AddNewTextBox() As System.Windows.Forms.TextBox
	Dim text As New System.Windows.Forms.TextBox()
	text.Name = "txt" + B
	Me.Controls.Add(text)
	textAdded.Insert(B, text)
	text.Top = B * 35
	text.Left = 150
	B = B + 1
	Return text
End Function
Private textAdded As New List(Of TextBox)()

Public Function AddNewButton() As System.Windows.Forms.Button
	Dim button As New System.Windows.Forms.Button()
	button.Name = "btn" + A
	button.Click += New EventHandler(AddressOf DeleteButton)
	Me.Controls.Add(button)
	button.Top = A * 35
	button.Left = 255
	button.Text = " Delete"
	A = A + 1
	Return button
End Function

Private Sub button1_Click(sender As Object, e As EventArgs)
	AddNewButton()
	AddNewTextBox()
End Sub

Private Sub DeleteButton(sender As Object, e As EventArgs)
	Dim btn As Button = TryCast(sender, Button)
	Dim name As String = btn.Name
	name = name.Replace("btn", "")
	Dim cntrls As Control() = Me.Controls.Find(Convert.ToString("txt") & name, False)
	Dim txt As TextBox = TryCast(cntrls(0), TextBox)
	If Me.Controls.Contains(txt) Then
		Me.Controls.Remove(txt)
		Me.Controls.Remove(btn)
	End If
End Sub

Screenshot