C# 子窗体与父窗体之间几种传值的方式
做了很多项目,很多项目都用到子父窗体之间的传值。。
父窗体传入子窗体都比较简单,而子窗体传入父窗体因为有很多不通道的需求,所以·搞起来有点头大。
先说父窗体传入子窗体:
将父窗体控件上的值传入子窗体的控件上:
Form1为父窗体
Form2为子窗体
Form1 单击按钮打开Form2,Form2关闭的时候把值传到Form1的控件上
代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Form2 f2 = new Form2(this.textBox1.Text);
- if (f2.ShowDialog() == DialogResult.OK)
- {
- this.textBox1.Text = f2.TextBoxValue;
- f2.Close();
- }
- }
- }
- }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(this.textBox1.Text); if (f2.ShowDialog() == DialogResult.OK) { this.textBox1.Text = f2.TextBoxValue; f2.Close(); } } } }
Form2的代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication3
- {
- public partial class Form2 : Form
- {
- public Form2():this(null)
- {
- }
- public string TextBoxValue
- {
- get { return textBox1.Text; }
- set { textBox1.Text = value; }
- }
- public Form2(string value) {
- InitializeComponent();
- TextBoxValue = value;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.OK;
- }
- }
- }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form2 : Form { public Form2():this(null) { } public string TextBoxValue { get { return textBox1.Text; } set { textBox1.Text = value; } } public Form2(string value) { InitializeComponent(); TextBoxValue = value; } private void button1_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; } } }
这是传值方式是最基本,租常见的一种,还有一种方式,就是打开子窗体Form2后,在往Form2上的控件上输入值的时候,Form1上的控件上的值也跟着变化,
对于这种情况,问你就需要使用 事件委托来完成。
----------------------------------------------------------快乐的分割线-------------------------------------------------------------
Form1代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Form2 f2 = new Form2();
- f2.TextBoxChanged += new EventHandler(
- (sender1, e1) =>
- { textBox2.Text = f2.TextBoxValue; }
- );
- f2.FormClosed += new FormClosedEventHandler(
- (sender2, e2) => { f2 = null; }
- );
- f2.Show(this);
- }
- }
- }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.TextBoxChanged += new EventHandler( (sender1, e1) => { textBox2.Text = f2.TextBoxValue; } ); f2.FormClosed += new FormClosedEventHandler( (sender2, e2) => { f2 = null; } ); f2.Show(this); } } }
Form2代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication3
- {
- public partial class Form2 : Form
- {
- public Form2():this(null)
- {
- }
- public string TextBoxValue
- {
- get { return textBox1.Text; }
- set { textBox1.Text = value; }
- }
- public event EventHandler TextBoxChanged;
- public Form2(string value) {
- InitializeComponent();
- TextBoxValue = value;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.OK;
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- if (TextBoxChanged != null)
- {
- TextBoxChanged(this, e);
- }
- }
- }
- }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form2 : Form { public Form2():this(null) { } public string TextBoxValue { get { return textBox1.Text; } set { textBox1.Text = value; } } public event EventHandler TextBoxChanged; public Form2(string value) { InitializeComponent(); TextBoxValue = value; } private void button1_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; } private void textBox1_TextChanged(object sender, EventArgs e) { if (TextBoxChanged != null) { TextBoxChanged(this, e); } } } }
这里需要使用 TextBox的TextChanged事件。
上面就是两种子窗体传入父窗体值的方法。