C#中用户控件与窗体间的消息传递如果在c#中实现用户定义控件内的事件挂勾到调用的窗体事件中我们都知道在开发中,如果使用用户定义控件可以有效的进行程序的模块化。
其实在.net 的winform中也是可以使用的。
细节如下:1、新建应用程序windowsapplication1。
2、添加新的用户控件userlogin。
(如图user)3、定义用户属性//定义属性public string username{get{return username;}set{username=value;}}public string password{get{return password;}set{password=value;}}4、定义委托//定义委托public delegate void btnokclickeventhander(object sender,eventargs e);public delegate void btncancelclickeventhander(object sender,eventargs e);5、定义事件//定义事件public event btnokclickeventhander btnokclick;public event btncancelclickeventhander btncancelclick6、事件实现private void textboxuid_textchanged(object sender, system.eventargs e){username=this.textboxuid.text;}private void textboxpwd_textchanged(object sender, system.eventargs e){password=this.textboxpwd.text;}private void buttonok_click(object sender, system.eventargs e){if (btnokclick!=null)btnokclick(this,e);}private void buttoncancel_click(object sender, system.eventargs e){if (btncancelclick!=null)btncancelclick(this,e);}7、在form1的winform中实现对用户控件事件的调用,消息的接收。
protected void okclick(object send,system.eventargs e){messagebox.show("uid:"+ername+";pwd:"+userlogin1.password,"userme ssage");}protected void cancelclick(object send,system.eventargs e){this.close();}8.按f5运行(如图result)附1(windowsapplication1源代码)using system;using system.drawing;using system.collections;using ponentmodel;using system.windows.forms;using system.data;namespace windowsapplication1{/// <summary>/// form1 的摘要说明。
/// </summary>public class form1 : system.windows.forms.form{private system.windows.forms.panel panel1;private erlogin userlogin1;/// <summary>/// 必需的设计器变量。
/// </summary>private ponentmodel.container components = null;public form1(){//// windows 窗体设计器支持所必需的//initializecomponent();userlogin1.btnokclick+=new btnokclickeventhander(okclick);userlogin1.btncancelclick+=new btncancelclickeventhander(cancelclick); //// todo: 在initializecomponent 调用后添加任何构造函数代码//}/// <summary>/// 清理所有正在使用的资源。
/// </summary>protected override void dispose( bool disposing ){if( disposing ){if (components != null){components.dispose();}}base.dispose( disposing );}#region windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法- 不要使用代码编辑器修改/// 此方法的内容。
/// </summary>private void initializecomponent(){this.panel1 = new system.windows.forms.panel();erlogin1 = new erlogin();this.panel1.suspendlayout();this.suspendlayout();//// panel1//this.panel1.controls.add(erlogin1);this.panel1.dock = system.windows.forms.dockstyle.fill;this.panel1.location = new system.drawing.point(0, 0); = "panel1";this.panel1.size = new system.drawing.size(216, 101);this.panel1.tabindex = 1;//// userlogin1//erlogin1.dock = system.windows.forms.dockstyle.fill;erlogin1.location = new system.drawing.point(0, 0); = "userlogin1";erlogin1.size = new system.drawing.size(216, 101);erlogin1.tabindex = 0;//// form1//this.autoscalebasesize = new system.drawing.size(6, 14);this.clientsize = new system.drawing.size(216, 101);this.controls.add(this.panel1); = "form1";this.text = "form1";this.panel1.resumelayout(false);this.resumelayout(false);}#endregion/// <summary>/// 应用程序的主入口点。
/// </summary>[stathread]static void main(){application.run(new form1());}protected void okclick(object send,system.eventargs e){messagebox.show("uid:"+ername+";pwd:"+userlogin1.password,"userme ssage");}protected void cancelclick(object send,system.eventargs e){this.close();}}}附2(userlogin源代码)using system;using system.collections;using ponentmodel;using system.drawing;using system.data;using system.windows.forms;namespace windowsapplication1{/// <summary>/// usercontrol1 的摘要说明。
/// </summary>/// //定义委托public delegate void btnokclickeventhander(object sender,eventargs e);public delegate void btncancelclickeventhander(object sender,eventargs e); public class userlogin : ercontrol{private string username,password;private system.windows.forms.groupbox groupbox1;private system.windows.forms.button buttoncancel;private system.windows.forms.button buttonok;private system.windows.forms.textbox textboxpwd;private bel label2;private bel label1;private system.windows.forms.textbox textboxuid;/// <summary>/// 必需的设计器变量。
/// </summary>private ponentmodel.container components = null;public userlogin(){// 该调用是windows.forms 窗体设计器所必需的。