Delete Control With Click In WindowsForms C #?

0

I need to click on a button, "select" that button, and by clicking on an erase button, that control will be deleted. How could I do that ?.

    
asked by Sebastián García 24.05.2018 в 03:19
source

1 answer

0
public partial class Form1 : Form
    {
        Button mLastSelection;
        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i < 5; i++)
            {
                Button btn = new Button();
                btn.Text = "Seleccione" + i;
                btn.Location = new Point(0, btn.Height * i);
                btn.Click += new EventHandler((sender, args) =>
                {
                    mLastSelection = sender as Button;
                });
                this.Controls.Add(btn);
            }

            Button borrarSeleccion = new Button();
            borrarSeleccion.Location = new Point(borrarSeleccion.Width + 15, 0);
            borrarSeleccion.Text = "Borrar Seleccionado";
            borrarSeleccion.Click += new EventHandler((sender, args) =>
            {
                mLastSelection?.Dispose();
            });

            this.Controls.Add(borrarSeleccion);
        }
    }
    
answered by 29.05.2018 в 04:54