[英]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.