【问题标题】:C# label control not working properlyC# 标签控件无法正常工作
【发布时间】:2013-09-12 13:31:04
【问题描述】:

当我运行以下代码时,它会填充一次label1 控件。然后label1 控件什么也不做。如何让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 = Alphabet.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);
            }
        }
    }

【问题讨论】:

  • 你试过什么?你的问题是什么 ? “请提供……”不是问题……
  • 请提供更少的 C# 和更多的英文。你到底想完成什么,什么是 GUI 框架? WinForms 的外观...
  • 当我输入其中一个面板 'chessBoardPanels[0,0] 或 chessBoardPanel [2,2] 我希望 label1 控件更改为当前棋盘位置坐标。网格实际上是一个 8 x 8 的网格
  • 类似于他几分钟前发布的this

标签: c# label


【解决方案1】:

我之所以回答这个问题,只是因为我认为展示范围混淆时会发生什么很重要。您遇到问题的原因是变量的范围。您的标签正在更改iRow * iColumn 次,但仅在初始执行期间。从那时起,iRowiColumn 将固定为其最终值。

要实现您想要的最终目标,创建 Panel 的扩展是最简单的:

public class ChessPanel : Panel {

     private const Color HighlightColor = Color.Red;


     public int iColumn { get; set; }
     public int iRow { get; set; }
     public Color PrimaryColor { get; set; }

     public ChessPanel() : base()
     {
        this.MouseEnter += (s,e) =>
                {
                    this.PrimaryColor = this.BackColor;
                    this.BackColor = HighlightColor;
                };

         this.MouseLeave += (s,e) => 
                {
                    this.BackColor = this.PrimaryColor;
                };
     }     

}    

这将允许您按如下方式减少代码:

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];
    ChessPanel[,] chessBoardPanels = new ChessPanel[numBlocks, numBlocks];
    string Alphabet = "A,B,C,D,E,F,G,H";
    string Numbers ="1,2,3,4,5,6,7,8";
    string[] alphaStrings = Alphabet.Split(',');
    string[] numStrings=Numbers.Split(',');
    int FirstValue, SecondValue;

    //Store Position Values --- no idea what this is supposed to do...
    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++)
        {
            ChessPanel p = new ChessPanel();
            //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);

            p.MouseEnter += (s,e) =>
            {
                var cpSelf = s as ChessPanel;
                if (cpSelf != null)
                {
                    label1.Text = Position[cpSelf.iRow, cpSelf.iColumn];
                }
            };

            groupBox1.Controls.Add(p);
            chessBoardPanels[iRow, iColumn] = p;
        }
    }
}

我假设您稍后在程序中使用了许多这些变量,并且看到这种情况让我头疼,所以我把大部分留在原地。

委托是一个非常强大和有用的实用程序,但它们可能会导致与变量范围混淆。使用这些时要非常小心,并确保将它们视为稍后执行的功能单元,因此只能依赖于块执行时的程序状态,而不是创建块时的状态。如果您注意到,我保留了对Position 数组的引用,只是为了表明您仍然可以访问委托范围内的局部变量,因为它在技术上仍在范围内。从结构上讲,这可以很容易地移入 ChessPanel 类并在本地 100% 引用。此示例应谨慎使用,因为它可以显示许多人认为在函数执行结束时被垃圾收集的“本地”变量如何徘徊并耗尽内存。

此代码未经测试,可能有轻微的语法错误。希望结构的精神得到理解。

【讨论】:

  • 是的,我理解你提到的关于变量超出范围的内容。这至少给了我一个方向。感谢您的帮助。
猜你喜欢
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
相关资源
最近更新 更多