Remove Dynamic Picturebox

Asked

Viewed 225 times

0

I have an "Add" button that when clicked opens a screen for the user to select an image, this image is added to a flowLayoutPanel. A dynamic "Remove" button is added to pictureBox, when this button is clicked to pictureBox should be removed.

I have this method:

private void AdicionarImagem()
        {
            try
            {
                DialogResult dr = new DialogResult();
                OpenFileDialog openDialog = new OpenFileDialog();
                openDialog.Title = "Localizar Foto";
                openDialog.Filter = "Arquivo (*.PNG)|*.PNG|Arquivo (*.JPG)|*.JPG|Arquivo (*.JPEG)|*.JPEG| All files(*.*) | *.* ";
                openDialog.CheckFileExists = true;
                openDialog.CheckPathExists = true;
                openDialog.InitialDirectory = "C:";
                openDialog.RestoreDirectory = true;
                dr = openDialog.ShowDialog();
                string arquivo = openDialog.FileName;

                if (dr.ToString().ToUpper() == "OK")
                {
                    if (String.IsNullOrEmpty(arquivo))
                    {
                        MessageBox.Show("Arquivo inválido.", "Arquivo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    else
                    {
                        var pb = new PictureBox();
                        pb.SizeMode = PictureBoxSizeMode.AutoSize;
                        pb.BorderStyle = BorderStyle.Fixed3D;
                        pb.Image = Image.FromFile(arquivo);
                        pb.Tag = img.ToString();
                        img++;
                        pb.Parent = flpFotos;

                        var btn = new Button();
                        btn.BackColor = Color.Red;
                        btn.ForeColor = Color.White;
                        btn.FlatStyle = FlatStyle.Popup;
                        btn.Text = "Remover";
                        btn.Size = new Size(80, 31);
                        btn.Parent = pb;

                        btn.Click += delegate
                        {
                        };

                        pb.Show();
                        btn.Show();
                    }
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show("Ocorreu o seguinte erro:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

The images are coming with the correct tag, in sequence (the first added pictureBox gets 1, the second gets 2 and...)

I was wondering if I could find a way to remove the picture Ox from her tag. Thank you!

  • try this: btn.Click += delegate
 {
 flpFotos.Controls.Remove(pb);
 };

  • It worked, thank you!

  • I put as a reply, thank you

1 answer

1


Just in the event of the button, you remove the pb of flpFotos:

btn.Click += delegate
{
    flpFotos.Controls.Remove(pb);
};

Browser other questions tagged

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