我有问题将图像设置为PictureBox如果它是空的...我尝试了几种不同的方法,我总是得到相同的异常。通过阅读这里和网上的文章,这应该有效,但它不会......
我已经宣布了6个PB阵列......
PictureBox[] red1 = new PictureBox[4];
PictureBox[] red2 = new PictureBox[4];
PictureBox[] red3 = new PictureBox[4];
PictureBox[] red4 = new PictureBox[4];
PictureBox[] red5 = new PictureBox[4];
PictureBox[] red6 = new PictureBox[4];
在FormLoad事件中,这些数组使用appropriet PBs填充......
PictureBox[] red1 = { pok11, pok12, pok13, pok14 };
PictureBox[] red2 = { pok21, pok22, pok23, pok24 };
PictureBox[] red3 = { pok31, pok32, pok33, pok34 };
PictureBox[] red4 = { pok41, pok42, pok43, pok44 };
PictureBox[] red5 = { pok51, pok52, pok53, pok54 };
PictureBox[] red6 = { pok61, pok62, pok63, pok64 };
当我调用方法来使用它们时会抛出异常......
呼叫:
DodajSLIKU(Properties.Resources.HERCv2, red1);
方法:
public void DodajSLIKU(Image slika, PictureBox[] t)
{
if (t[0].Image == null) //where exception occures.
t[0].Image = slika;
else if (t[1].Image == null)
t[1].Image = slika;
else if (t[2].Image == null)
t[2].Image = slika;
else if (t[3].Image == null)
t[3].Image = slika;
else
return;
}
我在哪里做错了?感谢...
答案 0 :(得分:0)
更改您的方法签名,如下所示:
public void DodajSLIKU(Image slika, PictureBox[] t)
{
foreach (var item in t)
{
if (item != null)
{
if (item.Image == null)
item.Image = slika;
}
}
}