I have a form and in the form I have a panel named : mypanel
now I want to write a class and in this class I want to use this:
foreach (Control cont in mypanel.Controls)
{
// do sth
}
but it is impossible, please help me how to access my form's panel in my class
I use this code :
foreach (Control cont in mypanel.Controls)
{
if (cont is PictureBox)
{
string path = @"" + Application.StartupPath + "\\Image\\";
cont.BackgroundImage = Image.FromFile(path + Rnd.Next(7).ToString() + ".png");
}
}
and I have this Error:
"the name 'mypanel' does not Exist in the current context"
Anonymous User
18-Dec-2013Check if the type of control is Panel and cast it.
You need to cast the control into PictureBox since not all control contains the specific property you want to set,
PictureBox pBox = (PictureBox)cont;
pBox.BackgroundImage = Image.FromFile(path + Rnd.Next(7).ToString() + ".png");