【转】WinForm窗体间数据交互的方法
在窗体间传递数据的方法比较多:
1.在子窗体中自定义一个构造函数,参数类型是主窗体,当要显示子窗体的时候,就用这个构造函数来实例化子窗体,然后把this指针传进去,说起来太抽象了,我大概一写大家应该就明白了:
public class frmMain:Form { ... frmControl controlForm=new frmControl(this); controlForm.Show(); } public class frmControl:Form //子窗体,用来控制主窗体的某些显示! { private frmMain mainForm; public frmControl(frmMain mainForm) { this.mainForm=mainForm; } private void button1_Click(object sender,EventArgs e) { frmMain.textBox1.Text=this.textBox1.Text; //把子窗体的文本框值传递给主窗体的文本框! } }
2,我个人感觉上面的方法不是很好,虽然实现起来很简单,只是想改变窗体的标题文本,就把整个主窗体的引用都传递给子窗体,这样的方式不是很幽雅,我们用接口来改进上面的方法,这样可以限制暴露给子窗体的功能,降低窗体之间的耦合度:
public interface IChangeTitle: { void ChangeTitle(string title); } public class frmMain:Form,IChangeTitle { ... frmControl controlForm=new frmControl(this); controlForm.Show(); public void ChangeTitle(string title) { this.Text=title; } } public class frmControl:Form //子窗体,用来控制主窗体的某些显示! { private IChangeTitle ichangeTitle; public frmControl(IChangeTitle ichangeTitle) { this.ichangeTitle=ichangeTitle; } private void button1_Click(object sender,EventArgs e) { ichangeTitle.ChangeTitle(this.textBox1.Text); //通过接口来调用方法 } }
3.为了进一步降低窗体之间的耦合度,我们可以用委托来实现这个需求:
public partial class ChildForm : Form { public delegate void TitleChangedHandler(string title); public TitleChangedEventHandler TitleChanged; public ChildForm() { InitializeComponent(); } private void btn_Ok_Click(object sender, EventArgs e) { if (TitleChanged != null) TitleChanged("Test Title"); //委托调用 } }
主窗体给委托变量赋值就可以了:
public partial class MainForm : Form { private ChildForm loginForm = new ChildForm(); public MainForm() { InitializeComponent(); loginForm.TitleChanged = new ChildForm.TitleChangedEventHandler(FormTitleChanged); } protected void FormTitleChanged(string title) { this.Text = title; } private void button1_Click(object sender, EventArgs e) { loginForm.Show(); } }
4.也可以在子窗体中定义一个自定义的事件,然后自定义一个事件参数,用来传递你想传递的一些信息:
public partial class ChildForm : Form { public class TitleChangedEventArgs : EventArgs //事件参数类 { private string title = ""; public string Title { get { return title; } set { title = value; } } } public delegate void TitleChangedEventHandler(object sender, TitleChangedEventArgs e); public event TitleChangedEventHandler TitleChanged; public ChildForm() { InitializeComponent(); } private void btn_Ok_Click(object sender, EventArgs e) { TitleChangedEventArgs e1=new TitleChangedEventArgs(); e1.Title="Login sucessed"; OnTitleChanged(e1);//触发事件 } protected virtual void OnTitleChanged(TitleChangedEventArgs e) //触发事件的方法 { if (TitleChanged != null) TitleChanged(this, e); } }
主窗体订阅这个事件就可以了:
public partial class MainForm : Form { private ChildForm loginForm = new ChildForm(); public MainForm() { InitializeComponent(); loginForm.TitleChanged += new ChildForm.TitleChangedEventHandler(FormTitleChanged); } protected void FormTitleChanged(object sender, ChildForm.TitleChangedEventArgs e) { this.Text = e.Title; } private void button1_Click(object sender, EventArgs e) { loginForm.Show(); } }
转自:http://www.xueit.com/cshare/show-7027-1.aspx