0

I want to add 23 picture boxes which I have been using in my form . I tried this way but it did not work:

List<PictureBox> pbList = new List<PictureBox>();

for (int i = 0; i < 23; i++)
  pbList.Add(pictureBox +i);  
cuongle
  • 74,024
  • 28
  • 151
  • 206
diego
  • 29
  • 1
  • 3
  • 7
  • Be careful using the `PictureBox` control, they can cause [memory leaks](http://stackoverflow.com/questions/1831732/c-sharp-picturebox-memory-releasing-problem) if you don't handle them correctly – musefan Sep 07 '12 at 11:32

2 Answers2

3

You are trying to add them by name, you have to do a find:

List<PictureBox> pbList = new List<PictureBox>();

for (int i = 0; i < 23; i++)
  pbList.Add((PictureBox)Controls.Find("pictureBox" + i)); 

Edit: If your picture boxes are nested within other controls, you will have to pass "true" to the Find() method so that all children are searched. The other solutions (including mine) will only search for picture boxes that are directly within the control container "Controls"

armen.shimoon
  • 6,303
  • 24
  • 32
  • I have problem ıt says= no overload for method 'find' takes 1 orguments. sory for my poor programing knowledge but ı couldnot find the problem – diego Sep 20 '12 at 10:52
0
var pbList = Controls.OfType<PictureBox>().Select(a => a.Name.StartsWith("pictureBox"));
Khurshid
  • 954
  • 7
  • 19