为什么标签不填充 [英] Why won't the label populate

查看:78
本文介绍了为什么标签不填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to create a chess strategy application. I seem to be having issues with trying to get the label1 control to populate during run time. I am actually pretty new to the idea of dynamically creating events like 'mouse enter, mouse leave' How do I get the label to show the coordinates in the mouse enter event

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

}

推荐答案

尝试此操作.由于未超出范围而未填充..iRow始终= 8 ... 将此类添加到您的项目中.

Try this.. It was not populating because of a out of range.. iRow always = 8... Add this class to your project.

    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方法:

Replace the FormLoad method with this:

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

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

And add those two methods to your code:

  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;
    }

我有几种方法可以实现...这很简单.

I there are several ways of doing it... This one is pretty straight forward.

这篇关于为什么标签不填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆