Reputation: 7301
Image property not found from the item variable. My code is -
foreach (Control item in this.Controls) //Iterating all controls on form
{
if (item is PictureBox)
{
if (item.Tag.ToString() == ipAddress + "OnOff")
{
MethodInvoker action = delegate
{ item.Image= }; //.Image property not shown
item.BeginInvoke(action);
break;
}
}
}
Any help please?
Upvotes: 2
Views: 195
Reputation: 1451
Use the as
operator, like so:
foreach (Control item in this.Controls)
{
PictureBox pictureBox = item as PictureBox;
if (pictureBox != null)
{
if (item.Tag.ToString() == ipAddress + "OnOff")
{
MethodInvoker action = delegate
{ item.Image= ... };
item.BeginInvoke(action);
break;
}
}
}
Upvotes: 1
Reputation: 13234
Your item
variable still is of the type Control
. Checking that the instance it is referencing is a PictureBox
does not change that. You could change your code to:
foreach (Control item in this.Controls) //Iterating all controls on form
{
var pb = item as PictureBox; // now you can access it a PictureBox, if it is one
if (pb != null)
{
if (item.Tag.ToString() == ipAddress + "OnOff")
{
MethodInvoker action = delegate
{
pb.Image = ... // works now
};
bp.BeginInvoke(action);
break;
}
}
}
Upvotes: 2