我正在制作一个小型的黑白棋/奥赛罗游戏,在我绘制棋盘的方法中,我有一个嵌套的for循环来显示一个PictureBoxes网格,每个都有一个图像。
第一次调用该方法时,所有PictureBox都被正确创建并且图像放在里面。但是,如果我再次调用该方法,我似乎无法覆盖已存在的图像。
我已经研究了一段时间,据我所知,它可能与PictureBox被锁定或需要在向其写入另一个图像之前正确Dispose()PictureBox有关。我无法获得任何这些解决方案,所以任何具体的帮助都会非常感激!
private void Draw()
{
Bitmap White = Properties.Resources.white;
Bitmap Black = Properties.Resources.black;
Bitmap None = Properties.Resources.none;
for (int r = 0; r <= grid.GetUpperBound(0); r++)
{
for (int c = 0; c <= grid.GetUpperBound(0); c++)
{
if (grid[r, c].value == 1)
{
var picbox = new PictureBox() // initialise picturebox for displaying images
{
Name = grid[r, c].name,
Size = new Size(64, 64),
Location = new Point(r * 65 + 15, c * 65 + 60),
Text = grid[r, c].name,
Image = White
};
Controls.Add(picbox); // add picturebox to form
picbox.Click += ClickBox;
MessageBox.Show("white draw" + grid[r, c].name);
}
if (grid[r, c].value == -1)
{
var picbox = new PictureBox()
{
Name = grid[r, c].name,
Size = new Size(64, 64),
Location = new Point(r * 65 + 15, c * 65 + 60),
Text = grid[r, c].name,
Image = Black
};
Controls.Add(picbox);
picbox.Click += ClickBox;
MessageBox.Show("black draw" + grid[r, c].name);
}
if (grid[r, c].value == 0)
{
var picbox = new PictureBox()
{
Name = grid[r, c].name,
Size = new Size(64, 64),
Location = new Point(r * 65 + 15, c * 65 + 60),
Text = grid[r, c].name,
Image = None
};
Controls.Add(picbox);
picbox.Click += ClickBox;
}
}
}
}
答案 0 :(得分:3)
问题是您创建了新的图片框,但没有从Controls
删除/更新现有的图片框。
首先,您需要找到要更新或从Controls
删除的图片框。
我建议创建一个图片框的网格,这样你就可以从网格中获取图片框。首先创造这个领域。
private var pictureGrid = new PictureBox[8, 8];
然后
if (grid[r, c].value == 1)
{
if (pictureGrid[r,c] != null)
{
pictureGrid[r,c].Image = White;
}
else
{
var picbox = new PictureBox()
{
Name = grid[r, c].name,
Size = new Size(64, 64),
Location = new Point(r * 65 + 15, c * 65 + 60),
Text = grid[r, c].name,
Image = White
};
pictureGrid[r,c] = picbox;
Controls.Add(picbox);
picbox.Click += ClickBox;
MessageBox.Show("white draw" + grid[r, c].name);
}
}
您还可以使用包含(grid.Value - Color)对的Dictionary。
private Dictionary<int, Bitmap> colors = new Dictionary<int, Bitmap>();
private void Load()
{
Bitmap White = Properties.Resources.white;
Bitmap Black = Properties.Resources.black;
Bitmap None = Properties.Resources.none;
colors.Add(1, White);
colors.Add(-1, Black);
colors.Add(0, None);
}
所以你的方法看起来像
private var pictureGrid = new PictureBox[8, 8];
private Dictionary<int, Bitmap> colors = new Dictionary<int, Bitmap>();
private void Load()
{
Bitmap White = Properties.Resources.white;
Bitmap Black = Properties.Resources.black;
Bitmap None = Properties.Resources.none;
colors.Add(1, White);
colors.Add(-1, Black);
colors.Add(0, None);
}
private void Draw()
{
for (int r = 0; r <= grid.GetUpperBound(0); r++)
{
for (int c = 0; c <= grid.GetUpperBound(0); c++)
{
if (pictureGrid[r, c] != null)
{
pictureGrid[r,c].Image = colors[grid[r,c]];
}
else
{
var picbox = new PictureBox()
{
Name = grid[r, c].name,
Size = new Size(64, 64),
Location = new Point(r * 65 + 15, c * 65 + 60),
Text = grid[r, c].name,
Image = colors[grid[r,c]]
};
pictureGrid[r,c] = picbox;
Controls.Add(picbox);
picbox.Click += ClickBox;
MessageBox.Show("black draw" + grid[r, c].name);
}
}
}
}
您还可以使用linq找到要更新的图片框
var pictureToRemove = this.Controls.OfType<PictureBox>().Where(x => x.Location.X == r * 65 + 15 && x.Location.Y == c * 65 + 60).First();