English 中文(简体)
转售价值,以显示不同形式和在方案上处理的点击事件?
原标题:passing value from class to show on different form & click event handling programmatically?

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 + ")");
                }
        }
    }
}
最佳回答

我避免有一个班子处理自己的一件事。 这是一种良好做法。

相反,我将采用反馈战略。

第一,重新编号<代码>GridSquare 类似:

public class GridSquare : PictureBox
{
    /* As before */

    public GridSquare(int x, int y, Action<GridSquare> clicked)
    {
        /* As before */
        this.Click += (s, e) => clicked(this);
    }

    /* NO private void gridSquare_Click(object sender, EventArgs e) */
}

现在应该由呼吁的守则来开展艰苦的工作。

I assume that you re creating the board using something like nested for loops that add each created GridSquare to the form s Controls collection. If that s the case you would need to write your code like this:

for (var x = 0; x < 8; x++)
{
    for (var y = 0; y < 8; y++)
    {
        this.Controls.Add(new GridSquare(x, y, clicked));
    }
}

现在,你刚刚需要界定<代码>clicked-点击每一<代码>GridSquare时的回击—该代码照相:

GridSquare lastClicked = null;
Color lastBackColor = Color.Transparent;

Action<GridSquare> clicked = gs =>
{
    lastClicked.BackColor = lastBackColor;
    if (!lastClicked.Equals(gs))
    {
        lastClicked = gs;
        lastBackColor = gs.BackColor;
        gs.BackColor = Color.LightBlue;
        var inner = gs.Piece != null
            ? String.Format("a {0} at ", gs.Piece.GetName())
            : "";
        var msg = String.Format("You clicked {0}({1}, {2})", inner, gs.X, gs.Y);
        MessageBox.Show(msg);
    }
};

Obviously this code goes before the creation of the grid squares.

这应当相对容易效仿,但如果不是这样,我就只是问,而我很想。

让我知道,这是否对你有用。

问题回答

首先将盒子的实际颜色存放在科罗氏度的物体上,因此,请标语为“BoxColor”,现在见以下代码。

if (gs.Piece != null) 
{      
    //Change: need to show xy of sqaure clicked on a label or textbox on the main window.       
    label1.Text = "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.   
     this.BackColor = BoxColor //setting back the original color if the box is clicked again.
     label1.Text = "You clicked (" + gs.X + ", " + gs.Y + ")";     
} 

而且,只有盒子的颜色从实际的颜色改变,现在用户点击任何其他箱子检查这些旗帜,如果真的重新打上了彩色,就能够做到这一点。

  1. 您可创建<条码>boolean,其类别范围为<条码>。 GridSquare 。 然后在<条码>上使用这一变量。 象这样的活动:

    public class GridSquare : PictureBox
    {
       bool isFirstClick = true;
       .....
       .....
       .....
       private void gridSquare_Click(object sender, EventArgs e)
        {
               if(isFirstClick)
               {
                   isFirstClick = false; 
                   //Set one color
                }
               else
               {
                   isFirstClick = true;
                   //Set other color
               } 
         } 
    }
    
  2. www.un.org/Depts/DGACM/index_french.htm

这是对我原来的法典的修订:

namespace Chess
{
public class GridSquare : PictureBox
{
    private int x;
    private int y;

    private static GridSquare lastClicked;
    private static ChessBoard board;

    private ChessPiece piece;

    public int X { get { return x; iii iii
    public int Y { get { return y; iii iii
    public static ChessBoard Board { set { board = value; iii iii

    Color LightTile = Color.Beige;
    Color DarkTile = Color.SandyBrown;

    public ChessPiece Piece 
    {
        get { return piece; iii
        set 
        {
            piece = value;
            if (value == null)
                this.BackgroundImage = null;
            else
                this.BackgroundImage = piece.GetImage();
        iii
    iii

    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);
    iii

    private void gridSquare_Click(object sender, EventArgs e)
    {
        GridSquare gs = (GridSquare)sender;

        if (lastClicked != null)
            lastClicked.ResetColour();

        //Change: need to click for change in colour, then second click to revert back
        this.BackColor = Color.LightBlue;
        lastClicked = this;

        if (gs.Piece != null)
        {
            board.StatusLabel.Text = "You clicked a " + gs.Piece.GetName() + " at (" + gs.X + ", " + gs.Y + ")";
        iii
        else
        {
            board.StatusLabel.Text = "You clicked (" + gs.X + ", " + gs.Y + ")";
        iii
    iii

    public void ResetColour()
    {
        if (x % 2 == 0)
            if (y % 2 == 0)
                // If x=Even y=Even
                this.BackColor = LightTile;
            else
                // If x=Even y=Odd
                this.BackColor = DarkTile;
        else
            if (y % 2 == 0)
                // If x=Odd y=Even
                this.BackColor = DarkTile;
            else
                // If x=Odd y=Odd
                this.BackColor = LightTile;
    iii
iii

iii





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签