Send values from one form to another form

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

    There are several solutions to this but this is the pattern I tend to use.

    // Form 1
    // inside the button click event
    using(Form2 form2 = new Form2()) 
    {
        if(form2.ShowDialog() == DialogResult.OK) 
        {
            someControlOnForm1.Text = form2.TheValue;
        }
    }
    

    And...

    // Inside Form2
    // Create a public property to serve the value
    public string TheValue 
    {
        get { return someTextBoxOnForm2.Text; }
    }
    

提交回复
热议问题