打个比方说,我想在程序中用
Label lab1 = new Label();
和Form1.controls.add(lab1); 方法添加一个label到窗体上,并同理添加一个Button到窗体上,然后Button得Click事件要MEssageBox.show出这个lab1的text属性,但是编译时C#提示这个lab1还为创建,不知道怎么解决这个问题,谢谢;
诚心求教!

解决方案 »

  1.   

    Label lab1 = new Label();改为:(1)添加类成员
    private Label lab1 = null;
    (2)
    修改你的函数
    Label lab1 = new Label();=》lab1 = new Label();OK
      

  2.   

    protected System.Windows.Forms.Button button
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            Label lable1 = new Label();
            Button btnadd = new Button();
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                this.axAXPlayer1.SetConnectString("10.34.78.10",8000,@"c:\");
               // this.axAXPlayer1.Play("10.34.78.13", 8000, @"c:\");
            }        private void Form1_Load(object sender, EventArgs e)
            {
              
                lable1.Text = "hello,world";
                this.Controls.Add(lable1);
                btnadd.Width = 20;
                btnadd.Height = 100;
                btnadd.Text = "测试";
                btnadd.Click += new EventHandler(btnadd_click);
                this.Controls.Add(btnadd);        }
            private void btnadd_click(object sender, EventArgs e)
            {
                MessageBox.Show(lable1.Text);
            }
        }
    }