Ryshad, that code looks good to me, but for some reason when I execute it (have only tried with one PictureBox added in the panel) the Remove(pic2remove)
method in the picture_click()
event doesn't seem to remove the control from the panel. I don't see why this wouldn't work and it relates to a similar post I made where you provided input as well... Any thoughts as to why this is?
Heres some code demonstrating how to name the control at creation and adding an event handler to its Click() event. I have used the click event to delete the control:
namespace WindowsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < 10; i++) { PictureBox pb = new PictureBox(); pb.Image = Properties.Resources.pic1; pb.Name = "pic" + i.ToString(); pb.Click += new EventHandler(picture_click); flowLayoutPanel1.Controls.Add(pb); } } void picture_click(object sender, EventArgs e) { PictureBox pic2remove = (PictureBox)sender; flowLayoutPanel1.Controls.Remove(pic2remove); } } }