Send values from one form to another form

后端 未结 19 2787
眼角桃花
眼角桃花 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:21

    you can pass as parameter the textbox of the Form1, like this:

    On Form 1 buttom handler:

    private void button2_Click(object sender, EventArgs e)
    {
    Form2 newWindow = new Form2(textBoxForReturnValue);
    newWindow.Show(); 
    }
    

    On the Form 2

    public static TextBox textBox2; // class atribute
    
    public Form2(TextBox textBoxForReturnValue)
    {
        textBox2= textBoxForReturnValue;
    }
    
    private void btnClose_Click(object sender, EventArgs e)
    {
    
        textBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim();
        this.Close();
    }
    

提交回复
热议问题