繁体   English   中英

在运行时PictureBox控件C中找不到图像属性

[英]Image property not found in runtime PictureBox control c#

从item变量中找不到图像属性。 我的代码是-

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;
        }
    }
}

有什么帮助吗?

您的item变量仍然是Control类型。 检查它所引用的实例是否为PictureBox不会改变它。 您可以将代码更改为:

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;
        }
    }
}

使用as运算符,如下所示:

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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM