Hi,
I have added no.of images(pictureboxes) in a flowlayoutpanel using openfiledialog.
Now i want to remove the particular control at runtime but i am able to get the particular control.
How can i name the pictureboxes while adding them in flowlayoutcontrol and after adding them how can i get the particular control by either selecting by mouseclick or anything else.
Can i select the added control by mouseclick.

Regards,
Vidya

Recommended Answers

All 8 Replies

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);
        }
    }
}
commented: good example! +1

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

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?

My apologies sir... That code works fine. I had my form and panel sized so small I didn't notice it was filling that location with next picture... :$ One of those moments for me I suppose....

Thanks To All,
The all given code works perfectly, and now i am able to name as well as remove the pictureboxes which i want.

Now, I want the name of the image which i add in flowlayout must get displayed along with that picturebox. For that purpose i have added label before adding picturebox which contain the name of the image.

But, now when i remove the picturebox, the label with the name get remained in the flowlayout. As i don't click on that label how can i delete that label.

if the picturebox name is pic0, then label of that picturebox name will be lblName0.

So, is there anything so that i can delete the label which matches the combination (pic0,lblName0 -pic1,lblName1).

Thanks,
Vidya

Glad the code helped you both. DdoubleD, no apology needed, we all have THOSE moments :p

And to Vidya, heres a revised version to add and remove a matching label. This method works if pic0 has a label named lbl0. There may be a better way to associate a label and a picture, i'll have a play around when i get 5 mins, but this should get you moving in the right direction.

Am i right in thinking this is a classroom exercise? If so, and you're new to C#, please remember to examine the code to make sure you understand it. If you just use it without understanding it you wont be able to apply the principles later :)

private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                Label lbl = new Label();
                lbl.Name = "lbl" + i.ToString();
                lbl.Text = "Picture #" + i.ToString();
                flowLayoutPanel1.Controls.Add(lbl);

                PictureBox pb = new PictureBox();
                pb.Image = Properties.Resources.pic;
                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);
            string lblName = pic2remove.Name;
            lblName = lblName.Replace("pic", "lbl");
            Label lbl2remove = (Label)flowLayoutPanel1.Controls[lblName];
            if (lbl2remove != null)
            {
                flowLayoutPanel1.Controls.Remove(lbl2remove);
            }
        }

Awesome, it really worked like a magic.....

The all pictureboxes which i have added in flowlayoutpanel, now i use to add those images path in database, i have did that all and along with adding in database i also have to copy those images to particular location suppose "D:\DataImages".

If the folder contain that image already then i use to delete that image and again add it to the same location..

This all works fine when the images/pictureboxes lodaded for first time in the flowlayoutpanel, but if the image is added in the flowlayoutpanel and we try to delete that image it shows error

"The file is used by another process" and i am not able to delete it, but before deleting those images i clear all controls from flowlayoutpanel, but still the error occurs... Can you help me on that...

Thnaks in advance,
Vidya

If the code supplied has answered your question, please mark this thread as solved.

You have a thread active for the issue with deleting, please keep issues seperate, if you start adding new issues to old threads they are less likely to be seen by solvers :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.