您的位置:首页 > 其它

在WinForms程序里实现窗体传值的最佳实践

2011-06-11 04:04 330 查看
为了实现这个例子,你需要准备2个窗口,一个叫MainForm,上面至少需要一个richTextBox,两个工具栏按钮。
另一个叫 MyDialog 的子窗口,上面有一个 textBox1,一个 Button,作为确定按钮。

两个工具栏按钮分别实现两种形式的窗体调用,模态的和非模态的。

模态的意思是,我们打开对话框,将值传进取,操作完成确定,主窗体再获得对话框的值。
非模态的意思是,我们打开对话框,可以在不关闭窗口的情况下和主窗体交互,主窗体可以即时获得子窗体的值。类似记事本的查找替换对话框。



主窗体:

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();
            }
        }
    }
}


子窗体:

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();
        }
    }
}


简单分析下这个代码:

模态传值的方法是:传入时可以使用构造函数,传出的时候首先判断是否用户是通过确定关闭的,如果是,那么用属性传出。
这个做法也是框架库的做法,比如打开文件对话框。

非模态的情况略微复杂:因为我们需要主窗体能和子窗体实时交互,为了同步主窗体和子窗体的数据,我们用了事件。有人问了,为什么我们不能让子窗体直接操作主窗体,这是因为考虑到对话框可以被重用,如果让它直接操作主窗口那么就限制死了这个子窗口只能被某个特定的主窗口调用。为了解除子窗体对调用者的耦合,我们使用事件。如果子窗体已经被显示,主窗体再次调用子窗体,那么通常我们希望激活子窗体而不是再显示一个。具体的实现参考代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: