簡體   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