如何用委托与事件实现winfrom窗体之间值传递
OutDataEventArgs.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PassFormDemo
{
public class OutDataEventArgs : EventArgs
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="refRecv"></param>
public OutDataEventArgs(string refRecv)
{
this.Recv = refRecv;
}
/// <summary>
/// 接受
/// </summary>
public string Recv { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PassFormDemo
{
public class OutDataEventArgs : EventArgs
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="refRecv"></param>
public OutDataEventArgs(string refRecv)
{
this.Recv = refRecv;
}
/// <summary>
/// 接受
/// </summary>
public string Recv { get; set; }
}
}
发送窗体:frmSend.cs
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 PassFormDemo
{
public partial class frmSend : Form
{
public frmSend()
{
InitializeComponent();
}
/// <summary>
/// 委托输出文本框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void SendTextBoxHandler(object sender, OutDataEventArgs e);
/// <summary>
/// 事件
/// </summary>
public event SendTextBoxHandler OutDataForm;
/// <summary>
/// 传递值
/// </summary>
/// <param name="Send"></param>
/// <param name="Recv"></param>
public void OutText(string Recv)
{
if (OutDataForm != null)
{
OutDataEventArgs args = new OutDataEventArgs(Recv);
OutDataForm(this, args);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
OutText(txtSend.Text);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PassFormDemo
{
public partial class frmSend : Form
{
public frmSend()
{
InitializeComponent();
}
/// <summary>
/// 委托输出文本框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void SendTextBoxHandler(object sender, OutDataEventArgs e);
/// <summary>
/// 事件
/// </summary>
public event SendTextBoxHandler OutDataForm;
/// <summary>
/// 传递值
/// </summary>
/// <param name="Send"></param>
/// <param name="Recv"></param>
public void OutText(string Recv)
{
if (OutDataForm != null)
{
OutDataEventArgs args = new OutDataEventArgs(Recv);
OutDataForm(this, args);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
OutText(txtSend.Text);
}
}
}
接受数据窗体:RecvFrm.cs
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 PassFormDemo
{
public partial class RecvFrm : Form
{
public RecvFrm()
{
InitializeComponent();
}
private frmSend frmsender = new frmSend();
private void RecvFrm_Load(object sender, EventArgs e)
{
frmsender.Show(); //展示窗体
//注册方法
frmsender.OutDataForm += new frmSend.SendTextBoxHandler((sender1, args) =>
{
txtRecv.Text += args.Recv + "\r\n";
});
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PassFormDemo
{
public partial class RecvFrm : Form
{
public RecvFrm()
{
InitializeComponent();
}
private frmSend frmsender = new frmSend();
private void RecvFrm_Load(object sender, EventArgs e)
{
frmsender.Show(); //展示窗体
//注册方法
frmsender.OutDataForm += new frmSend.SendTextBoxHandler((sender1, args) =>
{
txtRecv.Text += args.Recv + "\r\n";
});
}
}
}
源代码下载: