winfrom 控制窗体间相互传递数据
方法一
在子窗体中自定义一个构造函数,参数类型是主窗体,当要显示子窗体的时候,就用这个构造函数来实例化子窗体,然后把this指针传进去。代码如下:
主窗体.cs
public class frmMain:Form
{
...
frmControl controlForm=new frmControl(this);
controlForm.Show();
}
复制代码
子窗体.cs
子窗体.cs
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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;
//把子窗体的文本框值传递给主窗体的文本框!
}
}
复制代码
方法二
其实方法一不是很好,虽然实现起来很简单,只是想改变窗体的标题文本,就把整个主窗体的引用都传递给子窗体,这样的方式不是很幽雅,我们用接口来改进上面的方法,这样可以限制暴露给子窗体的功能,降低窗体之间的耦合度。代码如下:
定义接口.cs
public inte易做图ce IChangeTitle:
{
void ChangeTitle(string title);
}
复制代码
主窗体.cs
主窗体.cs
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public class frmMain:Form,IChangeTitle
{
...
frmControl controlForm=new frmControl(this);
controlForm.Show();
public void ChangeTitle(string title)
{
this.Text=title;
}
}
复制代码
子窗体.cs
子窗体.cs
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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);
}
}
复制代码
方法三
为了进一步降低窗体之间的耦合度,我们可以用委托来实现这个需求。代码如下:
主窗体.cs
主窗体.cs
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->// 主窗体给委托变量赋值就可以了:
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
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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"); //委托调用
}
}
复制代码
方法四
也可以在子窗体中定义一个自定义的事件,然后自定义一个事件参数,用来传递你想传递的一些信息。代码如下:
主窗体.cs
主窗体.cs
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->// 主窗体订阅这个事件就可以了
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
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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);
}
}
摘自 Bychentufeiyang的专栏
补充:软件开发 , C# ,