c# - 在运行时 PictureBox 控件 c# 中找不到图像属性

标签 c# winforms

未从项目变量中找到图像属性。我的代码是 -

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

关于c# - 在运行时 PictureBox 控件 c# 中找不到图像属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29874089/

相关文章:

c# - 如何在 MVVM 中禁用通过 ObjectProvider Enum WPF C# 提供的 ComboBoxItem?

c# - 如果从 IEnumerator<T>.MoveNext() 或 .Current 引发异常,是否仍必须处理该异常?

c# - 如何在不执行 Queryable 的情况下以通用方式替换 Queryable<T> 中的列

c# - 如何在 winform c# 中禁用工具条下的行?

c# - 来自非 web.page 类的 Http 当前请求

c# - HttpMessageHandler 应该使用 ConfigureAwait(false)

winforms - F# : what is the deal with the flicker? 中的双缓冲

c# - 滚动到 C# DataGridView 的底部

c# - 如何在 C# 中的 Windows 窗体应用程序中检查另一个按钮单击事件中的按钮是否被单击

c# - 将 RTSP 存储到文件位置