如何打开新表单,传递参数并返回参数

时间:2014-03-02 20:56:16

标签: c# winforms

在我的申请中,我打开新表格:

private void button1_Click(object sender, EventArgs e)
{
    Form2 = new Form2 ("bla bla");
    Form2 .ShowDialog();
}

这是我打开的表格,想要传回参数:

public partial class Form2: Form
{
    public Form2 (string file)
    {
        InitializeComponent();
    }
}

1 个答案:

答案 0 :(得分:7)

您可以定义要在Form2中返回的公共变量,并在Form1中访问它们:

public partial class Form2: Form
{
    public int x;    //can be private too
    public string y; //can be private too

    public Form2 (string file)
    {
        InitializeComponent();
    }

    //define some function which changes defined global values
}

在Form1中:

Form2 form2 = new Form2("bla bla");
form2.ShowDialog();
MessageBox.Show(form2.x.ToString());
MessageBox.Show(form2.y);