有模窗体:指的是一定要在第二个窗体上操作之后 才能去操作前者窗体 :例如,如果一个对话框,在可以切换到其它窗体或对话框之前要求先单击“确定”或“取消”,则它就是模式的。
无模窗体:可以同时进行两个窗体的操作 比如word中的find对话框;
窗体传值例程:
主窗体程序:
/// <summary> /// 有模窗体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click(object sender, EventArgs e) { MyDialog md = new MyDialog(); md.TextValue = richTextBox1.Text; if (md.ShowDialog() == DialogResult.OK) { richTextBox1.Text = md.TextValue; } } private MyDialog md; /// <summary> /// 无模窗体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click(object sender, EventArgs e) { if (md == null) { md = new MyDialog(); md.TextBoxChanged += new EventHandler((sender1, e1) => { richTextBox1.Text =md.TextValue; }); md.FormClosed += new FormClosedEventHandler((sender2, e2) => { md = null; }); md.Show(this); } else { md.Activate();//激活窗体 给予焦点 } }
子窗体程序:
public partial class MyDialog : Form { public MyDialog() { InitializeComponent(); } /// <summary> /// textBox1的属性设置 /// </summary> public string TextValue { get { return textBox1.Text; } set { textBox1.Text = value; } } private void button1_Click(object sender, EventArgs e) { Close(); } public event EventHandler TextBoxChanged; private void textBox1_TextChanged(object sender, EventArgs e) { if (TextBoxChanged != null) { TextBoxChanged(this, e); } }