Send values from one form to another form

后端 未结 19 2784
眼角桃花
眼角桃花 2020-11-21 11:45

I want to pass values between two Forms (c#). How can I do it?

I have two forms: Form1 and Form2.

Form1 contains one button. When I click on that button, Fo

19条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 12:03

    Form1 Code :

    private void button1_Click(object sender, EventArgs e)
    {
                Form2 f2 = new Form2();
                f2.ShowDialog();
                MessageBox.Show("Form1 Message :"+Form2.t.Text); //can put label also in form 1 to show the value got from form2
    }
    

    Form2 Code :

            public Form2()
            {
                InitializeComponent();
                t = textBox1;                        //Initialize with static textbox
            }
            public static TextBox t=new TextBox();   //make static to get the same value as inserted
            private void button1_Click(object sender, EventArgs e)
            {
    
                this.Close();
    
            }
    

    It Works!

提交回复
热议问题