C# 中窗体间传值总结

对C#窗体间传值做一些总结,记录如下 :

新建Form1为父窗口,Form2为子窗口。

父窗口向子窗口传值

相对而言父窗体向子窗体传值比较容易实现。

通过重载Form2的构造函数,使其能够接受来自Form1的参数。

Form1:

1
2
3
4
5
private void button1_Click(object sender, EventArgs e) {
string s = textBox1.Text;
Form2 form2 = new Form2(s);
form2.ShowDialog();
}

Form2:

1
2
3
4
public Form2(String p) {
InitializeComponent();
label1.Text = p;
}

通过声明全局变量

Form1:

1
2
3
4
5
6
public static string ss;
private void button1_Click(object sender, EventArgs e) {
ss = textBox1.Text;
Form2 form2 = new Form2();
form2.ShowDialog();
}

Form2:

1
2
3
4
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = Form1.ss;
}

或者

Form1:

1
2
3
4
5
6
7
public string Xx ;
private void button1_Click(object sender, EventArgs e) {
Xx = textBox1.Text;
Form2 form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}

Form2:

1
2
3
4
5
private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
label1.Text = f1.Xx;
}

通过声明公有属性

在Form2中声明公有属性,在Form1中访问公有属性,赋值。

Form2:

1
2
3
4
5
6
7
8
9
10
private string name;
public string strinS
{
get { return name; }
set { name = value; }
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = name;
}

Form1:

1
2
3
4
5
private void button1_Click(object sender, EventArgs e) {
Form2 form2 = new Form2();
form2.strinS = textBox1.Text;
form2.ShowDialog();
}

子窗体向父窗体传值

通过委托进行传值

点击Form1的按键,跳出Form2, 当Form2子窗体的文本框 内的值变化时,父窗体跟着发生变化。
Form2:

1
2
3
4
5
6
7
8
9
10
public delegate void MessageHandle(string s);
public MessageHandle messHandle;

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (messHandle != null)
{
messHandle(textBox1.Text.ToString());
}
}

Form1:

1
2
3
4
5
6
7
8
9
private void button1_Click(object sender, EventArgs e) {
Form2 f2 = new Form2();
f2.messHandle = getVaule;
f2.ShowDialog();
}
private void getVaule(string s)
{
label1.Text = s;
}

通过控件赋值

点击Form1的按键,跳出Form2, 在Form2的textbox中输入值,点击按钮,将值传入到Form1的文本框,关闭Form2。
Form1:

1
2
3
4
5
6
7
8
9
10
11
12
13
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Form2 f2 = new Form2();
f2.ShowDialog(this); //or f2.show(this);
// or
// f2.Owner = this;
// f2.Show(); or f2.ShowDialog();
}
}

Form2:

1
2
3
4
5
6
7
8
9
10
11
public Form2() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
//如果textBox 在Panel中,则需要先定位到Panel
Panel p1 = (Panel)(form1.Controls["panel1"]);
((TextBox)p1.Controls["textBox1"]).Text = this.textBox1.Text;
this.Close();
}

通过事件进行传值

点击Form1的按键,跳出Form2, 在Form2的textbox中输入值,点击按钮,将值传入到Form1的文本框。

Form1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public Form1() {
InitializeComponent();
}
private void getVaule(Object sender, EventArgs e)
{
Form2 f2 = (Form2)sender; //事件的接收者通过类型转换得到Form2的引用
this.textBox1.Text = f2.textValue; // //接收到Form2的textBox1的值
}

private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.accept += new EventHandler(getVaule);
f2.ShowDialog();
}

Form2 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Form2() {
InitializeComponent();
}

public string textValue {
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}

public event EventHandler accept;

private void button1_Click(object sender, EventArgs e)
{
if (accept != null)
{
accept(this, EventArgs.Empty); //触发事件时,传递自身引用
}
}

如果觉得对您有帮助,就扫我交个朋友吧!