一、接口实现窗体之间传值
1、定义接口IMessageOn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloMessage
{
public interface IMessageOn
{
void RecieveMsg(string strMsg);
}
}
2、定义子窗体FormA、FormB、FormC
(1)窗体FormA定义的接口IMessageOn类型的集合MessageOnList,并在构造器中初始化集合,最后夺按钮单击事件中进行传值
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
//初始化集合
MessageOnList = new List<IMessageOn>();
}
//接口的集合
public List<IMessageOn> MessageOnList { get; set; }
private void ButtonInterface_Click(object sender, EventArgs e)
{
foreach (var messageOn in MessageOnList)
{
messageOn.RecieveMsg("我是窗体A");
}
}
}
}
(2)窗体FormB实现接口
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormB : Form,IMessageOn
{
public FormB()
{
InitializeComponent();
}
public void RecieveMsg(string strMsg)
{
this.TextBoxB.Text = strMsg + DateTime.Now.ToString();
}
}
}
(3)窗体FormC实现接口
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormC : Form,IMessageOn
{
public FormC()
{
InitializeComponent();
}
public void RecieveMsg(string strMsg)
{
this.TextBoxC.Text = strMsg + DateTime.Now.ToString();
}
}
}
3、主窗体MainForm弹出所有子窗体FormA、FormB、FormC
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ButtonOpenForm_Click(object sender, EventArgs e)
{
FormA formA = new FormA();
formA.Show();
FormB formB = new FormB();
//用接口的形式注册观察者
formA.MessageOnList.Add(formB);
formB.Show();
FormC formC = new FormC();
formA.MessageOnList.Add(formC);
formC.Show();
}
}
}
要点:FormB和FormC都实现了接口的方法,并且都在主窗体(中介)里添加到了FormA的接口类型的集合IMessageOnList中。所以,当FormA在自己的按钮事件里遍历MessageOnList集合对象的时候,实际上用的是FormB和FormC的引用实例,给其传值后,FormB和FormC窗体的TextBox进行接收。
二、委托实现窗体之间传值
1、窗体MainForm客户定义委托类型,在按钮方法中将FormB和FormC中的方法挂接给FormA中的委托类型的属性SendMesgDelInstance
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
//定义委托类型
public delegate void SendMesgDel(string str);
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ButtonOpenForm_Click(object sender, EventArgs e)
{
FormA formA = new FormA();
formA.Show();
FormB formB = new FormB();
//把窗体B的方法放到窗体A的委托中去
formA.SendMesgDelInstance += formB.RecieveMsgDel;
formB.Show();
FormC formC = new FormC();
formA.SendMesgDelInstance += formC.RecieveMsgDel;
formC.Show();
}
}
}
2、窗体FormA中定义委托类型的属性,并在按钮事件中调用该委托,将TextBox中的值进行传递
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}
#region 委托实现传值
//定义委托类型的属性
public SendMesgDel SendMesgDelInstance { get; set; }
private void ButtonDelegate_Click(object sender, EventArgs e)
{
SendMesgDelInstance.Invoke(this.TextBoxA.Text);
}
#endregion
}
}
3、FormB和FormC窗体中的方法响应FormA的委托
(1)FormB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
}
//委托的方法
public void RecieveMsgDel(string strMsg)
{
this.TextBoxB.Text = strMsg + DateTime.Now.ToString();
}
}
}
(2)FromC
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormC : Form
{
public FormC()
{
InitializeComponent();
}
//委托的方法
public void RecieveMsgDel(string strMsg)
{
this.TextBoxC.Text = strMsg + DateTime.Now.ToString();
}
}
}
三、事件实现窗体传值
委托传值,因为委托类型是公开的(Public),并不安全,所以用事件进行传值更加合适。
1、窗体FormA中定义事件SendMesgDelEvent,并在按钮事件中触发该事件,进行传值
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}
#region 事件实现传值
//定义事件
public event SendMesgDel SendMesgDelEvent;
private void ButtonEvent_Click(object sender, EventArgs e)
{
SendMesgDelEvent(this.TextBoxA.Text);
}
#endregion
}
}
2、窗体FormB和FormC定义响应事件的方法
(1)FormB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
}
//事件的方法
public void RecieveMsgDelEvent(string strMsg)
{
this.TextBoxB.Text = strMsg + DateTime.Now.ToString();
}
}
}
(2)FormC
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
public partial class FormC : Form
{
public FormC()
{
InitializeComponent();
}
//事件的方法
public void RecieveMsgDelEvent(string strMsg)
{
this.TextBoxC.Text = strMsg + DateTime.Now.ToString();
}
}
}
3、MainForm中将FormB和FormC的方法挂接到FormA的事件上
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloMessage
{
//定义委托类型
public delegate void SendMesgDel(string str);
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ButtonOpenForm_Click(object sender, EventArgs e)
{
FormA formA = new FormA();
formA.Show();
FormB formB = new FormB();
formA.SendMesgDelEvent += formB.RecieveMsgDelEvent;
formB.Show();
FormC formC = new FormC();
formA.SendMesgDelEvent += formC.RecieveMsgDelEvent;
formC.Show();
}
}
}