Remove components dynamically in C #

2

Good night, I'm trying to solve a problem I encountered when trying to dynamically remove the picturebox I created:

    namespace WindowsFormsApplication8
    {
        public partial class Form1 : Form
        {
            int count = 0;
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

                //  pictureBox1.Controls.Add(pictureBox2);
                //pictureBox2.Location = new Point(0, 0);
                // pictureBox2.BackColor = Color.Transparent;
            }

            public void drawimg(int x, int y, bool clear_)
            {
                PictureBox marker = new PictureBox();
                if (!clear_)
                {
                    count++;
                    marker.Location = new Point(x, y);
                    marker.Name = "marker" + count.ToString();
                    marker.Size = new Size(25, 25);
                    marker.BackColor = Color.Transparent;
                    marker.Image = Resource1.marcador;
                    marker.SizeMode = PictureBoxSizeMode.Zoom;
                    pictureBox1.Controls.Add(marker);
                }
                else
                {
                    foreach (Control control in this.Controls)
                    {
                        if (control is PictureBox)
                        {
                            for (int i = 0; i <= count; i++)
                            {
                                if (control.Name == "marker" + i.ToString())
                                {
                                    this.Controls.Remove(control);
 //Não funciona, e gostaria de apenas remover as pictureboxes que eu criei //não todas...
               }
                            }


                        }
                    }

                }

            }

            private void button1_Click(object sender, EventArgs e)
            {
                int locX_, locY_;

                switch (DistritosList.GetItemText(DistritosList.SelectedItem))
                {

                    case "Porto":
                        locX_ = 10;
                        locY_ = 10;
                        drawimg(locX_, locY_, false);

                        break;
                    case "Aveiro":

                        locX_ = 50;
                        locY_ = 50;
                        drawimg(locX_, locY_, false);
                        break;
                    case "Coimbra":
                        drawimg(0, 0, true);
                        break;
                    case "Leiria":

                        break;
                }
            }


        }
    }

Any suggestions or criticisms are always welcome!

    
asked by anonymous 20.11.2017 / 23:51

1 answer

0

Create a list and store the ones you've created:

    List<PictureBox> pbs = new List<PictureBox>();

    public void drawimg(int x, int y, bool clear_)
    {
        PictureBox marker = new PictureBox();
        if (!clear_)
        {
            count++;
            marker.Location = new Point(x, y);
            marker.Name = "marker" + count.ToString();
            marker.Size = new Size(25, 25);
            marker.BackColor = Color.Transparent;
            marker.Image = Resource1.marcador;
            marker.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.Controls.Add(marker);
            pbs.Add(marker); //adiciona o pb à lista
        }
        else
        {
            //Quando for limpar, percorre a sua lista:
            foreach (PictureBox p in pbs)
            {
                this.Controls.Remove(p);
                p.Dispose();
            }
            pbs.Clear(); //limpa a lista
        }

     }
    
21.11.2017 / 00:09