How to pass string value from one form to another form's load event in C #

后端 未结 5 509
我寻月下人不归
我寻月下人不归 2020-12-03 15:12

I need to pass a string value from Form1:

public void button1_Click(object sender, EventArgs e)
{
    string DepartmentName = \"IT\";
    Form2          


        
5条回答
  •  时光说笑
    2020-12-03 15:56

    Just create a property on the Form2 class and set it before you show Form2.

    public class Form2
    {
       ...
       public string MyProperty { get; set; }
    
       private void Form2_Load(object sender, EventArgs e)
       {
           MessageBox.Show(this.MyProperty);
       }
    }
    

    From Form1:

    public void button1_Click(object sender, EventArgs e)
    {
        string departmentName = "IT";
        Form2 frm2 = new Form2();
        frm2.MyProperty = departmentName;
        frm2.Show();
        this.Hide();
    }
    

提交回复
热议问题