I found that I can’t use the Image property for a PictureBox control in List<Control>.
I want to do something like this:
List<Control> pictureboxes = new List<Control>();
private void button1_Click(object sender, EventArgs e)
{
foreach (var picturebox in pictureboxes)
{
picturebox.Image = WindowsFormsApplication1.Properties.Resources.image;
}
}
Can I somehow do that?
The reason that you can’t access the
Imageproperty of yourPictureBoxcontrols when you place them in aList<Control>container is because the base type of the list (Control) doesn’t have anImageproperty.The property didn’t disappear, though. You just have to cast the object from a
Controlto a more derived class,PictureBox. Then you can call whatever methods or access whatever properties you want. For example: