-1

我一直在尝试创建一个国际象棋策略应用程序。我似乎在尝试让 label1 控件在运行时填充时遇到问题。实际上,我对动态创建诸如“鼠标进入,鼠标离开”之类的事件的想法很陌生我如何获得标签以显示鼠标进入事件中的坐标

int currentXposition, currentYposition;

const string positionLabel = "Current Position: ";

private void Test_Load(object sender, EventArgs a)
{
    var temp=Color.Transparent;    //Used to store the old color name of the panels before mouse events
    var colorName = Color.Red;      //Color used to highlight panel when mouse over
    int numBlocks = 8;             //Used to hold the number of blocks per row
    int blockSize=70;

    //Initialize new array of Panels  new

    string[,] Position = new string[8, 8];

    Panel[,] chessBoardPanels = new Panel[numBlocks, numBlocks];

    string Alphabet = "A,B,C,D,E,F,G,H";

    string Numbers ="1,2,3,4,5,6,7,8";

    string[] alphaStrings = Numbers.Split(',');

    string[] numStrings=Numbers.Split(',');

    // b = sub[0];

    int FirstValue, SecondValue;           

    //Store Position Values
    for (int firstValue = 0; firstValue < 8; ++firstValue)
    {
        FirstValue = Alphabet[firstValue];               

        for (int SecValue = 0; SecValue < 8; ++SecValue)
        {
            SecondValue = Numbers[SecValue];
            Position[firstValue, SecValue] = alphaStrings[firstValue] + numStrings[SecValue];
        }
    }

    //Loop to create panels
    for (int iRow = 0; iRow < numBlocks; iRow++)
        for (int iColumn = 0; iColumn < numBlocks; iColumn++)
        {
            Panel p = new Panel();
            //set size
            p.Size = new Size(blockSize, blockSize);
            //set back colour
            p.BackColor = (iRow + (iColumn % 2)) % 2 == 0 ? Color.Black : Color.White;
            //set location
            p.Location = new Point(blockSize *iRow+15, blockSize * iColumn+15);
            chessBoardPanels[iRow, iColumn] = p;
            chessBoardPanels[iRow,iColumn].MouseEnter += (s,e) =>
            {
                currentXposition = iRow;
                currentYposition = iColumn;
                var oldColor = (s as Panel).BackColor;
                (s as Panel).BackColor = colorName;
                temp = oldColor;
                label1.Text = Position[iRow, iColumn];
            };

            chessBoardPanels[iRow, iColumn].MouseLeave += (s, e) => 
            {
                (s as Panel).BackColor = temp;
            }; 
            groupBox1.Controls.Add(p);
        }

}
4

1 回答 1

0

试试这个.. 由于超出范围而没有填充.. iRow always = 8... 将此类添加到您的项目中。

    public class ChessSquare
    {
        public string Letter { get; set; }
        public int Number { get; set; }

        public Color Color { get; set; }

        public string Position
        {
            get { return string.Format("{0}{1}", Letter, Number); }
        }

        public ChessSquare()
        {
        }

        public ChessSquare(string letter, int number)
        {
            Letter = letter;
            Number = number;
        }
    }

将 FormLoad 方法替换为:

        int blockSize = 20;

        Panel[,] chessBoardPanels = new Panel[8, 8];

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                ChessSquare sq = new ChessSquare(((char)(65+i)).ToString(), j);
                sq.Color = (i + (j % 2)) % 2 == 0 ? Color.AliceBlue : Color.White;

                Panel p = new Panel() 
                    {   Size = new Size(blockSize, blockSize), 
                        BackColor = sq.Color, 
                        Tag = sq,
                        Location = new Point(blockSize * i + 15, blockSize * j+15),
                    };

                p.MouseEnter+=new EventHandler(squareMouseEnter);
                p.MouseLeave += new EventHandler(squareMouseLeave);

                chessBoardPanels[i, j] = p;
                groupBox1.Controls.Add(p);
            }
        }

并将这两种方法添加到您的代码中:

  void squareMouseEnter(object sender, EventArgs e)
    {
        Panel p = (Panel)sender;
        ChessSquare sq = (ChessSquare)p.Tag;
        p.BackColor = Color.Aqua;
        label1.Text = string.Format("Current position: {0}", sq.Position);
    }

    void squareMouseLeave(object sender, EventArgs e)
    {
        Panel p = (Panel) sender;
        ChessSquare sq = (ChessSquare)p.Tag;
        p.BackColor = sq.Color;
    }

我有几种方法可以做到这一点......这个很简单。

于 2013-09-12T18:03:17.873 回答