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 :
问题1:如果再次点击同一qa或不同的qa,怎么能够把颜色改回原样?
问题2:在文本箱或标签上显示表格1.c的信息,如何不展示什么信息箱?
下面是我从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 + ")");
}
}
}
}