I have placed 16 pictureboxes on my form and now I want to give them some images with something like this pictureBox[i] , pictureBox[i+2] and etc. I tried using picturebox array but it doesn't work for me, I made everything as in example on the internet and still nothing. Please explain me in more detail way if possible.
Asked
Active
Viewed 177 times
-1
-
Please take a moment to review this *carefully*: [Ask] – Ňɏssa Pøngjǣrdenlarp Mar 19 '15 at 19:33
-
What code do you have so far ? – Philip Stuyck Mar 19 '15 at 19:35
2 Answers
0
Iterate through the controls of the parent form will do the trick nicely:
foreach(Control control in this.Controls){
if (control is PictureBox){
//do stuff.
}
}

Philip Stuyck
- 7,344
- 3
- 28
- 39
-
This breaks down if OP uses a picturebox for any other purpose or requires all to be nested inside the same control container. – Chris Marisic Mar 19 '15 at 19:53
-
Not if you are smart enough to group the pictureboxes that are alike in a panel and then iterate through the panel. – Philip Stuyck Mar 19 '15 at 19:56
-1
Create the array by hand with fixed references. Suppose you name them PictureBox1, PictureBox2
var pictureBoxes = new [] { PictureBox1, PictureBox2, ... PictureBox16 };

Chris Marisic
- 32,487
- 24
- 164
- 258
-
-
-
-
1yes it would because this is a form of more tight coupling. Someone who adds a picturebox has to know he has to add it to your array as well. Hence there are two related actions. Just mere adding the picture box in the designer already breaks the code. You assume that anyone who adds a picturebox to a form, will add it to your array. This assumption is likely to catch up with you. – Philip Stuyck Mar 19 '15 at 22:35