1、前言
在WinForm
开发中,窗体之间的相互传值是一项基本操作,本文就来介绍一下实现方法。
2、父窗体传值给子窗体
父窗体传值给子窗体比较简单,可以利用构造函数、属性、自定义函数实现。新建一个WinForm
项目,创建父窗体MainForm
和子窗体ChildForm
,如下图所示:
MainForm
界面如下所示:
ChildForm
界面如下所示:
2.1、构造函数传值
MainForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class MainForm : Form
{
private ChildForm childForm;
public MainForm()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new ChildForm(txtName.Text);
childForm.Show();
}
}
}
}
ChildForm
代码如下:
using System.Windows.Forms;
namespace App
{
public partial class ChildForm : Form
{
public ChildForm(string currentText)
{
InitializeComponent();
lblName.Text = currentText;
}
}
}
2.2、属性传值
MainForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class MainForm : Form
{
private ChildForm childForm;
public MainForm()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new ChildForm();
childForm.CurrentText = txtName.Text;
childForm.Show();
}
}
}
}
ChildForm
代码如下:
using System.Windows.Forms;
namespace App
{
public partial class ChildForm : Form
{
public string CurrentText
{
get { return lblName.Text; }
set { lblName.Text = value; }
}
public ChildForm()
{
InitializeComponent();
}
}
}
2.3、自定义函数传值
MainForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class MainForm : Form
{
private ChildForm childForm;
public MainForm()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new ChildForm();
childForm.SetText(txtName.Text);
childForm.Show();
}
}
}
}
ChildForm
代码如下:
using System.Windows.Forms;
namespace App
{
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
public void SetText(string currentText)
{
lblName.Text = currentText;
}
}
}
以上三种方法的效果是一样的,如下图所示:
3、子窗体传值给父窗体
子窗体传值给父窗体需要使用委托和事件实现。新建一个WinForm
项目,创建父窗体MainForm
和子窗体ChildForm
,如下图所示:
MainForm
界面如下所示:
ChildForm
界面如下所示:
3.1、自定义委托
MainForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class MainForm : Form
{
private ChildForm childForm;
public MainForm()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new ChildForm();
childForm.handler = new SetTextEventHandler(SetText);
childForm.Show();
}
}
private void SetText(string text)
{
lblName.Text = text;
}
}
}
ChildForm
代码如下:
using System.Windows.Forms;
namespace App
{
public delegate void SetTextEventHandler(string text);
public partial class ChildForm : Form
{
public SetTextEventHandler handler;
public ChildForm()
{
InitializeComponent();
}
private void btn_Click(object sender, System.EventArgs e)
{
if (handler != null)
{
handler(txtName.Text);
}
}
}
}
3.2、自定义委托+事件
MainForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class MainForm : Form
{
private ChildForm childForm;
public MainForm()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new ChildForm();
childForm.handler += new SetTextEventHandler(SetText);
childForm.Show();
}
}
private void SetText(string text)
{
lblName.Text = text;
}
}
}
ChildForm
代码如下:
using System.Windows.Forms;
namespace App
{
public delegate void SetTextEventHandler(string text);
public partial class ChildForm : Form
{
public event SetTextEventHandler handler;
public ChildForm()
{
InitializeComponent();
}
private void btn_Click(object sender, System.EventArgs e)
{
if (handler != null)
{
handler(txtName.Text);
}
}
}
}
3.3、内置委托
MainForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class MainForm : Form
{
private ChildForm childForm;
public MainForm()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new ChildForm();
childForm.handler = new Action<string>(SetText);
childForm.Show();
}
}
private void SetText(string text)
{
lblName.Text = text;
}
}
}
ChildForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class ChildForm : Form
{
public Action<string> handler;
public ChildForm()
{
InitializeComponent();
}
private void btn_Click(object sender, System.EventArgs e)
{
if (handler != null)
{
handler(txtName.Text);
}
}
}
}
3.4、内置委托+事件
MainForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class MainForm : Form
{
private ChildForm childForm;
public MainForm()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new ChildForm();
childForm.handler += new Action<string>(SetText);
childForm.Show();
}
}
private void SetText(string text)
{
lblName.Text = text;
}
}
}
ChildForm
代码如下:
using System;
using System.Windows.Forms;
namespace App
{
public partial class ChildForm : Form
{
public event Action<string> handler;
public ChildForm()
{
InitializeComponent();
}
private void btn_Click(object sender, System.EventArgs e)
{
if (handler != null)
{
handler(txtName.Text);
}
}
}
}
以上四种写法的效果是一样的,如下图所示:
4、结语
窗体传值是WinForm
中的基础问题,因此开发者需要熟练掌握委托、事件、属性的使用方法。