please consider that this is my first very post and that I´m a newcomer in C#
I’m creating a UserControl with several pictureBoxes. These objects are automatically named pictureBox1, pictureBox2, pictureBox3 and so on.
Now i want to change the associated image by clicking:
pictureBox1.Image = Properties.Resources.i51;
so far, so good. But how to assigne the number without writing a single line for each event?
pictureBox(1).Image = Properties.Resources.i51;
doesn´t work, and lots of others don´t too.
any hint? Many thanks in advance!
Fill all your picture boxes into a list like msmolka already provided and also add a ReadyOnlyCollection to it:
Instantiate it within the constructor:
And make the ReadOnlyCollection only available as
IList:This is (in my eyes) better than the approach from msmolka. For making it perfect i would also not give the whole PictureBoxes to the outer world, but instead only the
Images as aBindingList. But i think for a beginner you can start with the above and maybe improve this stuff later.