【发布时间】:2020-02-29 06:53:13
【问题描述】:
char SideA = 'A';
char SideB = 'B';
int CPUPlayer = 1;
Test.Text = x.ToString();
if (difficulty == 1)
{
if (CPUPlayer == 1)
{
string targetString = "";
for (int side = 1; side <= 1; side++)
{
targetString = SideA.ToString();
Test.Text = targetString.ToString();
for (int game = 1; game < 25; game++)
{
targetString = game.ToString();
for (int tile = 1; tile < 10; tile++)
{
targetString = tile.ToString();
PictureBox target = (PictureBox)(this.Controls.Find(targetString, true))[0];
if (target.BackgroundImage == null)
{
target.BackgroundImage = Properties.Resources.smallo;
Test.Text = targetString.ToString();
}
}
}
}
}
}
所以我试图循环通过预先命名的图片框(例如:A11、A12、A13....等),让它们通过使用循环字符串连接到图片框来更改背景图像,但我不断收到@987654323 @来自PictureBox target = (PictureBox)(this.Controls.Find(targetString, true))[0];
我不知道该怎么做才能修复它。我对 c# 和一般编码非常陌生,所以很高兴知道我缺少什么!
【问题讨论】:
-
显然
this.Controls.Find(targetString, true)在某些情况下是一个空数组。 -
你需要将
PictureBox target = (PictureBox)(this.Controls.Find(targetString, true))[0]分解成多个语句和look at the values in a debugger。 -
将图像缓存在数组中,而不是像那样使用来自
Resources的图像。当一个对象会做并且会导致泄漏时,您会不必要地创建大量对象
标签: c# for-loop indexoutofboundsexception picturebox