winfrom窗体 子窗体和父窗体实时传值。
using System; using System.Collections.Generic; using System.Text; namespace WF.ThreadFrom { public delegate void TextChangedEventHander(object sender,TextChangedEventEventArgs e); public class TextChangedEventEventArgs:EventArgs { private string msg; public string Msg { get { return msg; } } public TextChangedEventEventArgs(string message) { this.msg = message; } } } using System; using System.Collections.Generic; using System.Text; namespace WF.ThreadFrom { public delegate void SelectionChangedEventHandler(object sender, SelectionChangedEventArgs e); public class SelectionChangedEventArgs:EventArgs { private string m_selection; //本属性用于传递事件数据 public string Selection { get { return m_selection; } } public SelectionChangedEventArgs(string selection) { m_selection = selection; } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WF.ThreadFrom { public partial class Form2 : Form { public event SelectionChangedEventHandler SelectionChanged; public event TextChangedEventHander TextChanged; public Form2() { InitializeComponent(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (SelectionChanged != null) { SelectionChangedEventArgs E = new SelectionChangedEventArgs(comboBox1.Text); SelectionChanged(this, E); } } private void textBox1_TextChanged(object sender, EventArgs e) { if (TextChanged != null) { TextChangedEventEventArgs E = new TextChangedEventEventArgs(textBox1.Text); TextChanged(this, E); } } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WF.ThreadFrom { public partial class Form1 : Form { private Form2 frm; public Form1() { InitializeComponent(); frm = new Form2(); frm.SelectionChanged += new SelectionChangedEventHandler(frm_SelectionChanged); frm.TextChanged += new TextChangedEventHander(frm_TextChanged); } void frm_TextChanged(object sender, TextChangedEventEventArgs e) { textBox1.Text = e.Msg; } void frm_SelectionChanged(object sender, SelectionChangedEventArgs e) { textBox1.Text = e.Selection; } private void button1_Click(object sender, EventArgs e) { frm.ShowDialog(); } private void textBox1_TextChanged(object sender, EventArgs e) { } } }