WinForm 参数传递

父窗体给子窗体传参

子窗体代码:
 //添加构造函数
public Form2(string param)
{
      InitializeComponent();
      textBox1.Text = param;
}

父窗体代码:
private void button2_Click(object sender, EventArgs e)
{
       Form2 f = new Form2(textBox1.Text);
       f.StartPosition = FormStartPosition.CenterParent;
       f.ShowDialog();
}

子窗体给父窗体传参

子窗体代码:
 //添加属性
        public string Parameter1
        {
            get { return textBox1.Text; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
父窗体代码:
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2(); //Form2为子窗体名称
            f.StartPosition = FormStartPosition.CenterParent;
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                button1.Text = f.Parameter1;
            }
        }

转载于:https://www.cnblogs.com/EasyData/archive/2008/03/30/1129927.html

你可能感兴趣的:(WinForm 参数传递)