0

Necesito que al dar click a un botón, "seleccione" ese botón, y al dar click a un botón de borrar, ese control se elimine. ¿Cómo podría realizar eso?.

3
  • 1
    Has intentado algo? Podrías usar una variable para almacenar el ultimo botón pulsado y eliminarlo posteriormente
    – Pikoh
    el 24 may. 2018 a las 8:39
  • Ya lo había pensado, pero que tipo de variable podría ser? el 24 may. 2018 a las 11:13
  • 1
    Ehmm...una variable de tipo Button por ejemplo? :)
    – Pikoh
    el 24 may. 2018 a las 11:18

1 respuesta 1

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);
        }
    }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.