Dynamically remove components in C#

Asked

Viewed 87 times

2

Good evening, I’m trying to solve a problem I came across by dynamically trying to 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!

  • This is Stackoverflow in English. Could you translate your question to English?

  • Okay, I just translated, I didn’t know there was in Portuguese...

  • What errors are you getting?

1 answer

0


Create a list and store the ones you 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
        }

     }
  • Thanks for the answer! It still doesn’t work though. 0 errors, just doesn’t erase.

  • 1

    I just solved :=) just needed to add p.Dispose();

  • below the remove ?

  • 1

    yes sir! :=)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.