简体   繁体   中英

Why can't I change this label with this button?

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

Because firstLabel is a local variable scoped to the mainWindow constructor. You could make firstLabel a private member of the mainWindow class:

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

Additionally, most classes are conventionally PascalCased and not camelCased (ie class MainWindow instead of mainWindow ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM