用事件进行窗口间参数传递
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 WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private Form2 m_dlg; private void button1_Click(object sender, EventArgs e) { Form2 dlg = new Form2(richTextBox1.Text); if (dlg.ShowDialog() == DialogResult.OK) { richTextBox1.Text = dlg.TextBoxValue; } } private void button2_Click(object sender, EventArgs e) { if (m_dlg == null) { m_dlg = new Form2(richTextBox1.Text); m_dlg.TextBoxChanged += new EventHandler<Form2.UpdataEventArgs>(m_dlg_textboxchanged); m_dlg.FormClosed += new FormClosedEventHandler( (sender2, e2) => { m_dlg = null; } ); m_dlg.Show(this); } else { m_dlg.Activate(); } } private void m_dlg_textboxchanged(object sender1, Form2.UpdataEventArgs e1) { richTextBox1.Text = e1.texBox; } } }
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 WindowsFormsApplication1 { public partial class Form2 : Form { public Form2(string str) { InitializeComponent(); TextBoxValue = str; } public event EventHandler<UpdataEventArgs> TextBoxChanged; private void Form2_Load(object sender, EventArgs e) { } public string TextBoxValue { get { return textBox1.Text; } set { textBox1.Text = value; } } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox1_TextChanged_1(object sender, EventArgs e) { if (TextBoxChanged != null) { UpdataEventArgs ex = new UpdataEventArgs(); ex.texBox = textBox1.Text; TextBoxChanged(this, ex); } } public class UpdataEventArgs:EventArgs { public string texBox; } } }
作者:wanglei_wan
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。