4-C#的不同窗口传值

C#的不同窗口传值

1.通过构造函数传值

this.Hide();
Form1 form01 = new Form1(textBox2.Text);
//Application.Run(form01);
form01.Show();
public Form1(string aaa)
{
    InitializeComponent();
    label12.Text = aaa;
}

2.全局类传值

namespace WindowsFormsApp1
{
    public class Global
    {
        //单例模式
        private  static Global _Instance=null;

        public static Global Instance
        {
            get 
            { 
                if (_Instance == null)
                    _Instance = new Global();
                return _Instance;
            }
        }

        public string name = "123";
    }
}

this.Hide();
Form1 form01 = new Form1();
Global.Instance.name = textBox2.Text;
form01.Show();
        private void Form1_Load(object sender, EventArgs e)
        {
            //SoundPlayer sd= new SoundPlayer();
            //sd.SoundLocation=
            label3.Text = DateTime.Now.ToString("yyyy:MM:dd:HH:mm:ss.ff");
            this.Text = "zijishengchang ";
            label12.Text = Global.Instance.name;
        }

你可能感兴趣的:(C#,c#,开发语言)