【问题标题】:Why can't I change this label with this button?为什么我不能用这个按钮改变这个标签?
【发布时间】:2012-12-23 04:26:39
【问题描述】:
using System;  
using System.Drawing;  
using System.Windows.Forms;  
namespace Test  
{  
    class mainWindow : Form  
    {  
        public mainWindow()  
        {  
            Label firstLabel = new label();  
            firstLabel.Text = "Hello";  
            this.Controls.Add(firstLabel);  
            Button firstButton = new Button();  
            firstButton.Text = "Click me";  
            firstButton.Click += firstButton_Click;  
            firstbutton.Left = 100;  
            this.Controls.Add(firstButton);  
        }  
        void firstButton_Click(object sender, EventArgs e)  
        {  
            firstlabel.Text = "Goodbye";  
        }  
    }  
    class XxX  
    {  
        static void Main()  
        {  
            mainWindow form = new mainWindow();  
            Application.Run(form);  
        }  
    }
}  

【问题讨论】:

  • 欢迎来到 StackOverflow!请在以后使用{ } 按钮格式化您的代码。
  • 您的代码无法编译。即使您修复了某些变量的大小写,您也需要将 firstlabel 声明为您的类的成员。
  • 由于您是在运行时动态创建控件对象,因此请确保您在表单上指定控件的坐标

标签: c# forms button label


【解决方案1】:

因为firstLabel 是作用域为mainWindow 构造函数的局部变量。您可以将 firstLabel 设为 mainWindow 类的私有成员:

class mainWindow : Form  
{  
    private Label firstLabel;
    public mainWindow()  
    {  
        firstLabel = new Label();  
        firstLabel.Text = "Hello";  
        this.Controls.Add(firstLabel);  
        Button firstButton = new Button();  
        firstButton.Text = "Click me";  
        firstButton.Click += firstButton_Click;  
        firstbutton.Left = 100;  
        this.Controls.Add(firstButton);  
    }  
    void firstButton_Click(object sender, EventArgs e)  
    {  
        firstLabel.Text = "Goodbye";  
    }  
}  

此外,大多数类通常是PascalCased 而不是camelCased(即class MainWindow 而不是mainWindow)。

【讨论】:

  • 我将 firstLabel 设置为该类的私有成员,但现在按下按钮时出现错误:MyFirstScratchApp.exe 中发生“System.NullReferenceException”类型的未处理异常附加信息:对象引用未设置为对象的实例。
  • 您是否发布了所有代码?按钮单击时唯一可能为空的是firstLabel,我们已经在mainWindow 构造函数中对其进行了实例化。
  • 我没有发布所有代码,我感到沮丧和愤怒退出,然后只为这个问题重新写了一小部分,我正在关注微软网站上的一个教程,展示如何制作一个数学游戏,但我想从头开始做,而不是从工具箱中点击和拖动东西,有很多代码......
  • @user1924485:明白了。我会尝试在点击处理程序中设置一个断点并逐步执行每一行。这样,您应该能够轻松分辨出哪个变量包含 null 引用。
  • 我刚刚检查了我的 IDE,它可以编译,所以我认为没有错误,但它说我的标签从未分配给它,并且它的默认值始终为 null。另外我对 C# 很陌生,我不知道断点是什么......
【解决方案2】:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
    class mainWindow : Form
    {
        // since u need to access the control, u need to keep that control at global level
        private Button firstButton = null;
        private Label firstLabel = null;
        public mainWindow()
        {
            firstLabel = new Label();
            firstLabel.Text = "Hello";
            Controls.Add(firstLabel);
            Button firstButton = new Button();
            firstButton.Text = "Click me";
            firstButton.Click += firstButton_Click;
            firstButton.Left = 100;
            Controls.Add(firstButton);
        }
        void firstButton_Click(object sender, EventArgs e)
        {
            // now since the variable is global u can access it and change the title
            firstLabel.Text = "Goodbye";
        }
    }
    static class XxX
    {
        static void Main()
        {
            mainWindow form = new mainWindow();
            Application.Run(form);
        }
    }
}

【讨论】:

  • 另一个重要的区别是全局变量在 C# 中不存在。而关于记忆的部分是不对的。 每个变量将变量名与内存位置相关联。
  • 我的意思是说它必须是该类的全局!
  • 如同,必须由包含程序的任何函数访问?
  • 如果它们处于类作用域级别而不是函数作用域变量
  • 首先,全局变量可以在 c# 中使用 internal/public 修饰符在公共静态类中定义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
相关资源
最近更新 更多