WinForm 窗体间传递数据的一些方法[转]
方法一
在子窗体中自定义一个构造函数,参数类型是主窗体,当要显示子窗体的时候,就用这个构造函数来实例化子窗体,然后把this指针传进去。代码如下:
主窗体.cs
public class frmMain:Form
{
...
frmControl controlForm=new frmControl(this);
controlForm.Show();
}
{
...
frmControl controlForm=new frmControl(this);
controlForm.Show();
}
子窗体.cs
子窗体.cs
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;
//把子窗体的文本框值传递给主窗体的文本框!
}
}
{
private frmMain mainForm;
public frmControl(frmMain mainForm)
{
this.mainForm=mainForm;
}
private void button1_Click(object sender,EventArgs e)
{
frmMain.textBox1.Text=this.textBox1.Text;
//把子窗体的文本框值传递给主窗体的文本框!
}
}
方法二
其实方法一不是很好,虽然实现起来很简单,只是想改变窗体的标题文本,就把整个主窗体的引用都传递给子窗体,这样的方式不是很幽雅,我们用接口来改进上面的方法,这样可以限制暴露给子窗体的功能,降低窗体之间的耦合度。代码如下:
定义接口.cs
public interface IChangeTitle:
{
void ChangeTitle(string title);
}
{
void ChangeTitle(string title);
}
主窗体.cs
主窗体.cs
public class frmMain:Form,IChangeTitle
{
...
frmControl controlForm=new frmControl(this);
controlForm.Show();
public void ChangeTitle(string title)
{
this.Text=title;
}
}
{
...
frmControl controlForm=new frmControl(this);
controlForm.Show();
public void ChangeTitle(string title)
{
this.Text=title;
}
}
子窗体.cs
子窗体.cs
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);
}
}
{
private IChangeTitle ichangeTitle;
public frmControl(IChangeTitle ichangeTitle)
{
this.ichangeTitle=ichangeTitle;
}
private void button1_Click(object sender,EventArgs e)
{
// 通过接口来调用方法
ichangeTitle.ChangeTitle(this.textBox1.Text);
}
}
方法三
为了进一步降低窗体之间的耦合度,我们可以用委托来实现这个需求。代码如下:
主窗体.cs
主窗体.cs
// 主窗体给委托变量赋值就可以了:
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();
}
}
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();
}
}
子窗体.cs
子窗体.cs
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 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"); //委托调用
}
}
方法四
也可以在子窗体中定义一个自定义的事件,然后自定义一个事件参数,用来传递你想传递的一些信息。代码如下:
主窗体.cs
主窗体.cs
// 主窗体订阅这个事件就可以了
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();
}
}
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();
}
}
子窗体.cs
子窗体.cs
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 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);
}
}
来源:http://www.cnblogs.com/xuefeng1982/archive/2010/06/11/1756620.html
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |