【发布时间】:2014-03-14 15:14:15
【问题描述】:
上下文:我正在尝试在 Windows.Forms 中制作一个双陆棋小游戏。我有 3 个类 GameBoard.cs、Points.cs 和 Checkers.cs(以及 Form1.cs)。我需要做的是使用我的其他一些类来更改 PictureBox 的某些属性的值。
特别是我有这个代码:
// gameBoard.cs
namespace backgammon
{
public class gameBoard
{
Checker checker1;
Points point1;
Points[] pointsArray;
public gameBoard()
{
// make new checker (ID, PictureBox, startingPoint)
checker1 = new Checker(1, checkerPicBox1, 1);
// make new Point (ID, arrayOfCheckers)
point13 = new Points(1, new Checker[]{checker1 /*,checker2... etc*/});
pointsArray = new Points[MAX_POINTS];
pointsArray[0] = point1;
}
}
}
这就是我“设置”跳棋和点的方式。我的 checker 和 point 类可以获取和设置传递到其构造中的所有变量。
问题:我想要实现的是在单击检查器后“突出显示”它。
在表格 1 中:
// Form1.cs
private void checkerPicBox1_Click(object sender, EventArgs e)
{
int pointNumber = gameBoard.checker1.getPointMember();
// find the top most checker in the checker array so we can highlight it
Checker topMost = gameBoard.pointsArray[pointNumber - 1].getCheckerFromIndex(gameBoard.pointsArray[pointNumber - 1].getCheckerArray().Length - 1);
// get the picturebox and change the image
topMost.getPictureBox().BackgroundImage = global::Backgammon.Properties.Resources.blackCheckerSelected;
}
代码编译并运行,但是当它到达 checkerPicBox_Click 的最后一行时,似乎什么也没发生(图像没有改变)。
这里发生了什么?我没有正确的 PictureBox 实例吗?还是我以一种奇怪的方式/不是我应该的方式这样做?
【问题讨论】:
-
getPictureBox()获得了什么图片框?是否等于null? -
在这个例子中(我正在搜索的检查器数组中唯一的检查器是
checker1)所以它会得到PictureBox与checker1相关联编辑 - 不,@987654328 @ 不返回 null。 -
topMost.getPictureBox().BackgroundImage = null;这段代码是否清空了图片框?还有一件事,你用的是变量i,它在哪里声明的? -
您可能需要致电
topmost.getPictureBox().Refresh() -
@DonBoitnott 或
topmost.getPictureBox().Invalidate();
标签: c# winforms picturebox