Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

c# - Why won't the label populate

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...