Send values from one form to another form

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

    Ok so Form1 has a textbox, first of all you have to set this Form1 textbox to public in textbox property.

    Code Form1:

    Public button1_click()
    {
        Form2 secondForm = new Form2(this);
        secondForm.Show();
    }
    

    Pass Form1 as this in the constructor.

    Code Form2:

    Private Form1 _firstForm;
    
    Public Form2(Form1 firstForm)
    {
        _firstForm = firstForm:
    }
    
    Public button_click()
    {
        _firstForm.textBox.text=label1.text;
        This.Close();
    }
    

提交回复
热议问题