I have 1 picturebox loaded with an image, what i'm trying to do is every time i click on loaded image the same picture loads into empty picturebox. Now for 1 picturebox is easy, all i do is picturebox1.Image = img;

I'm trying to make an event that will load image to next empty picture box
This code is in the picturebox.click event that is loaded with image

int counter = 0;

            List<PictureBox> pbox= new List<PictureBox>(); //Holds list of PictureBoxes that will be loaded(currently there are 28)
            PictureBox img = ((PictureBox) sender); //Holds the image that i clicked


            foreach (Control ctrl in groupBox1.Controls) //Enumerate all controls and add them to the list
            {
                if (ctrl is PictureBox)
                {                    
                    pbox.Add((PictureBox) ctrl);
                }

            }
            pbox[counter] = img; //loads the image to the first picturebox in list
            pictureBox1.Image = pbox[counter].Image; // Now this should load  the image (i need help with this), what should i use instead picturebox1.Image
            counter++;

For clarification...

You have say 5 picture boxes

[] [] [] [] []

You load an image into one

[x] [] [] [] []

and so you want the code to then automatically put the image into the next empty one?

[x] [x] [] [] []?

Edited 4 Years Ago by Mike Askew

Something like that.
It's supposed to load the same picture

Edited 4 Years Ago by Michael27

You will probably want to make use of the tag property to allow you to order the pictures boxes in some way. Then you need a search algorithm to find which is the nearest tag number to the selected one thats empty.

I do have to question however, as I'm playing around with code. What determines the next picture box.

In my example I have 8 picture boxes in the layout:
[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]

So say I click the image in:
[ ] [ ] [x] [ ]
[ ] [ ] [ ] [ ]

Which of the picture boxes would be the next one (assuming all are empty except [x])?
[a] [b] [x] [c]
[ ] [ ] [ ] [ ]

A, B or C?

Edited 4 Years Ago by Mike Askew

Everyone has to be filled
1 is loaded at startup and the rest is empty(and controls are in flow layout panel), then you need to load first in array, then second, then third etc

Which of the picture boxes would be the next one (assuming all are empty except [x])?
[a] [b] [x] [c]
[ ] [ ] [ ] [ ]

a and b has to filled before c is

i have picturebox [x]
and then empty ones like this
[] [] [] []
when i click on loaded one, first picturebox is loaded, when i click second time, second picturebox is loaded, third time the third picturebox is loaded etc.

Hope it is more clear now.

This article has been dead for over six months. Start a new discussion instead.