子窗体按钮事件
ChildWindow child = new ChildWindow() { textbox1.Text="我的名字是父窗体给的!" };//第1步,给子窗体传值了
child.ShowDialog();//第2步,调用ShowDialog
if (child.DialogResult==true)//第3步,然后对DialogResult进行判断
{ this.Title = child.Title;//子窗体给父窗体传值
}
父窗体按钮事件
textbox2.Text= "我要给子窗体传值";
this.DialogResult = true;//第3步,首先会话结束
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 传值练习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//1、利用构造函数由父窗体向子窗体传值
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this.textBox1.Text);
f2.Show();
}
//利用方法由子窗体向父窗体传值
public void chuanzhi(string data)
{
this.textBox1.Text = data;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 传值练习
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//1、利用构造函数由父窗体向子窗体传值
public Form2(string name)
{
InitializeComponent();
this.textBox1.Text = name;
}
//2、利用方法由子窗体向父窗体传值
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.chuanzhi(this.textBox1.Text);
f1.Show();
}
}
}
Form1的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyURLRecond
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form1(string message)
{
InitializeComponent();
textBox1.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(textBox1.Text.Trim());
form2.ShowDialog();
form2.Dispose();
}
}
}
Form2的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyURLRecond
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string message)
{
InitializeComponent();
textBox1.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1(textBox1.Text.Trim());
form1.ShowDialog();
form1.Dispose();
}
}
}