我正在制作一个小型Reversi / Othello游戏,在我的画板方法中,我有一个嵌套的for循环,用于显示PictureBox的网格,每个PictureBox都带有一个图像。
第一次调用该方法时,将正确创建所有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;
}
}
}
}
最佳答案
问题是您创建了新的图片框,但没有从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-颜色)对的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();
关于c# - 无法更新图片框内的图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36554310/