窗体传值

这个是子窗体的代码:

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 MyDialog : Form
{
public event EventHandler TextBoxChanged;

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

public MyDialog() : this("") { }

public MyDialog(string Param)
{
InitializeComponent();
TextBoxValue = Param;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (TextBoxChanged != null)
TextBoxChanged(this, e);
}

private void button1_Click(object sender, EventArgs e)
{
Close();
}
}
}

 

这是主窗体的代码:


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 MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private MyDialog m_dlg;

private void toolStripButton1_Click(object sender, EventArgs e)
{
MyDialog dlg = new MyDialog(richTextBox1.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
richTextBox1.Text = dlg.TextBoxValue;
}
}

private void toolStripButton2_Click(object sender, EventArgs e)
{
if (m_dlg == null)
{
m_dlg = new MyDialog(richTextBox1.Text);
m_dlg.TextBoxChanged += new EventHandler(
(sender1, e1) =>
{ richTextBox1.Text = m_dlg.TextBoxValue; }
);
m_dlg.FormClosed += new FormClosedEventHandler(
(sender2, e2) => { m_dlg = null; }
);
m_dlg.Show(this);
}
else
{
m_dlg.Activate();
}
}
}
}

posted on 2013-08-23 10:21  雪山飞石  阅读(181)  评论(0编辑  收藏  举报