从类传递值以不同的形式显示以编程方式处理单击事件?
我目前创建了一个国际象棋棋盘,其中填充了棋子的图像,并对棋盘进行了着色等,我在点击事件方法方面有两个问题,希望您能帮助我解决?当单击一个方块时,我当前的方法会更改方块颜色并显示一个消息框,其中显示 xy 坐标和块类型,或者如果它没有被块占据,则仅显示坐标:
问题 1:如何更改颜色如果我再次单击同一个正方形或不同的正方形,回到原来的状态?
问题 2:如何在文本框或标签中显示 Form1.cs 上的信息,而不是显示消息框?
以下是我的 GridSqaure.cs 类中的代码:
namespace Chess
{
public class GridSquare : PictureBox
{
private int x;
private int y;
private ChessPiece piece;
public int X { get { return x; } }
public int Y { get { return y; } }
public ChessPiece Piece
{
get { return piece; }
set
{
piece = value;
if (value == null)
this.BackgroundImage = null;
else
this.BackgroundImage = piece.GetImage();
}
}
public GridSquare(int x, int y)
{
int ButtonWidth = 64;
int ButtonHeight = 64;
int Distance = 20;
int start_x = 10;
int start_y = 10;
this.x = x;
this.y = y;
this.Top = start_x + (x * ButtonHeight + Distance + x);
this.Left = start_y + (y * ButtonWidth + Distance + y);
this.Width = ButtonWidth;
this.Height = ButtonHeight;
this.Text = "X: " + x.ToString() + " Y: " + y.ToString();
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Click += new System.EventHandler(gridSquare_Click);
}
private void gridSquare_Click(object sender, EventArgs e)
{
GridSquare gs = (GridSquare)sender;
if (gs.Piece != null)
{
//Change: need to show xy of sqaure clicked on a label or textbox on the main window.
MessageBox.Show("You clicked a " + gs.Piece.GetName() + " at (" + gs.X + ", " + gs.Y + ")");
//Change: need to click for change in colour, then second click to revert back
this.BackColor = Color.LightBlue;
}
else
{
//Change: need to show xy of clicked square but including meesage stating the sqaure needs to be occupied by a piece.
MessageBox.Show("You clicked (" + gs.X + ", " + gs.Y + ")");
}
}
}
}
ive currently created a chess board populated with images of the pieces in place and have coloured the board etc, i have 2 issues with the click event method of which im hoping you could help me on? when a square is clicked my current method changes the sqaure colour and displays a messagebox showing the xy co-ordinates and type of piece or just co-ordinates if its not occupied by a piece :
Issue 1 : How can i get the colour to change back to its original if i click the same sqaure again or a different sqaure?
Issue 2 : How would i instead of showing a meesagebox, display the information on Form1.cs in a textbox or label?
The following is my code from my GridSqaure.cs class:
namespace Chess
{
public class GridSquare : PictureBox
{
private int x;
private int y;
private ChessPiece piece;
public int X { get { return x; } }
public int Y { get { return y; } }
public ChessPiece Piece
{
get { return piece; }
set
{
piece = value;
if (value == null)
this.BackgroundImage = null;
else
this.BackgroundImage = piece.GetImage();
}
}
public GridSquare(int x, int y)
{
int ButtonWidth = 64;
int ButtonHeight = 64;
int Distance = 20;
int start_x = 10;
int start_y = 10;
this.x = x;
this.y = y;
this.Top = start_x + (x * ButtonHeight + Distance + x);
this.Left = start_y + (y * ButtonWidth + Distance + y);
this.Width = ButtonWidth;
this.Height = ButtonHeight;
this.Text = "X: " + x.ToString() + " Y: " + y.ToString();
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Click += new System.EventHandler(gridSquare_Click);
}
private void gridSquare_Click(object sender, EventArgs e)
{
GridSquare gs = (GridSquare)sender;
if (gs.Piece != null)
{
//Change: need to show xy of sqaure clicked on a label or textbox on the main window.
MessageBox.Show("You clicked a " + gs.Piece.GetName() + " at (" + gs.X + ", " + gs.Y + ")");
//Change: need to click for change in colour, then second click to revert back
this.BackColor = Color.LightBlue;
}
else
{
//Change: need to show xy of clicked square but including meesage stating the sqaure needs to be occupied by a piece.
MessageBox.Show("You clicked (" + gs.X + ", " + gs.Y + ")");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会避免让一个类处理它自己的事件之一。这不是一个好的做法。
相反,我会使用回调策略。
首先,像这样重新编码 GridSquare:
现在由调用代码来完成艰苦的工作。
我假设您正在使用类似嵌套 for 循环之类的东西来创建板,该循环将每个创建的 GridSquare 添加到表单的 Controls 集合中。如果是这种情况,您需要像这样编写代码:
现在您只需要定义
clicked
- 单击每个GridSquare
时的回调 - 该代码如下所示:显然,这段代码是在创建网格方块之前执行的。
这应该相对容易理解,但如果不是,请询问,我会详细说明。
让我知道这是否适合您。
I would avoid having a class handle one of its own events. It isn't a good practice.
Instead, I would use a callback strategy.
First, recode
GridSquare
like this:Now it is up to the calling code to do the hard work.
I assume that you're creating the board using something like nested for loops that add each created
GridSquare
to the form'sControls
collection. If that's the case you would need to write your code like this:Now you just need to define
clicked
- the callback when eachGridSquare
is clicked - and that code looks like this:Obviously this code goes before the creation of the grid squares.
This should be relatively easy to follow, but if not, just ask and I'll elaborate.
Let me know if this works for you.
首先将盒子的实际颜色存储在 Color 类的对象中,假设对象名称为“BoxColor”,现在看下面的代码。
并且还放置一个布尔变量,仅当框的颜色从其实际颜色更改时才启用它,现在如果用户单击任何其他框,请检查这些标志,如果其为 true,则重置颜色。
First store the actual color of the box in an object of Color class, lets assume the object name is "BoxColor" and now see the following code.
And also put one boolean variable enable it only when the color of the box is changed from its actual color and now if the user clicks on any other boxes check for these flag and if its true reset the color.
您可以在类
GridSquare
的范围内创建一个boolean
变量。然后在gridSquare_Click
事件处理程序上使用此变量,如下所示:只需使用
Label
或TextBox
的Text
属性.You can create a
boolean
variable on the scope of your classGridSquare
. Then use this variable ongridSquare_Click
event handler like this:Simply use
Text
property ofLabel
orTextBox
.这就是我将原始代码修改为:
}
This is what i ammended my original code to:
}