【asp精品源码栏目提醒】:网学会员在asp精品源码频道为大家收集整理了“【精品】C#中用户控件与窗体间的消息传递 - 其它资料“提供大家参考,希望对大家有所帮助!
C中用户控件与窗体间的消息传递如果在 c中实现用户定义控件内的事件挂勾到调用的窗体事件中 我们都知道在 asp.net 开发中,如果使用用户定义控件可以有效的进行程序的模块化。
其实在.net 的 winform 中也是可以使用的。
细节如下:1、新建应用程序 windowsapplication1。
2、添加新的用户控件 userlogin。
(如图 user)3、定义用户属性 //定义属性 public string username getreturn username setusernamevalue public string password getreturn password setpasswordvalue 4、定义委托//定义委托public delegate void btnokclickeventhanderobject sendereventargs epublic delegate void btncancelclickeventhanderobject sendereventargs e 5、定义事件 //定义事件 public event btnokclickeventhander btnokclick public event btncancelclickeventhander btncancelclick6、事件实现private void textboxuid_textchangedobject sender system.eventargs e usernamethis.textboxuid.text private void textboxpwd_textchangedobject sender system.eventargs e passwordthis.textboxpwd.text private void buttonok_clickobject sender system.eventargs e if btnokclicknull btnokclickthise private void buttoncancel_clickobject sender system.eventargs e if btncancelclicknull btncancelclickthise 7、在 form1 的 winform 中实现对用户控件事件的调用,消息的接收。
protected void okclickobject sendsystem.eventargs e messagebox.showuid:userlogin1.usernamepwd:userlogin1.passwordusermessage protected void cancelclickobject sendsystem.eventargs e this.close 8.按 f5 运行如图 result附 1(windowsapplication1 源代码)using systemusing system.drawingusing system.collectionsusing system.componentmodelusing system.windows.formsusing system.datanamespace windowsapplication1////// form1 的摘要说明。
///public class form1 : system.windows.forms.form private system.windows.forms.panel panel1 private windowsapplication1.userlogin userlogin1 /// /// 必需的设计器变量。
/// private system.componentmodel.container components null public form1 // // windows 窗体设计器支持所必需的 //initializecomponentuserlogin1.btnokclicknew btnokclickeventhanderokclickuserlogin1.btncancelclicknew btncancelclickeventhandercancelclick//// todo: 在 initializecomponent 调用后添加任何构造函数代码//////// 清理所有正在使用的资源。
///protected override void dispose bool disposing if disposing if components null components.dispose base.dispose disposing region windows 窗体设计器生成的代码////// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。
///private void initializecomponentthis.panel1 new system.windows.forms.panelthis.userlogin1 new windowsapplication1.userloginthis.panel1.suspendlayoutthis.suspendlayout//// panel1//this.panel1.controls.addthis.userlogin1this.panel1.dock system.windows.forms.dockstyle.fillthis.panel1.location new system.drawing.point0 0this.panel1.name panel1this.panel1.size new system.drawing.size216 101this.panel1.tabindex 1//// userlogin1 // this.userlogin1.dock system.windows.forms.dockstyle.fill this.userlogin1.location new system.drawing.point0 0 this.userlogin1.name userlogin1 this.userlogin1.size new system.drawing.size216 101 this.userlogin1.tabindex 0 // // form1 // this.autoscalebasesize new system.drawing.size6 14 this.clientsize new system.drawing.size216 101 this.controls.addthis.panel1 this.name form1 this.text form1 this.panel1.resumelayoutfalse this.resumelayoutfalse endregion /// /// 应用程序的主入口点。
/// stathread static void main application.runnew form1 protected void okclickobject sendsystem.eventargs e messagebox.showuid:userlogin1.usernamepwd:userlogin1.passwordusermessage protected void cancelclickobject sendsystem.eventargs e this.close 附 2(userlogin 源代码)using systemusing system.collectionsusing system.componentmodelusing system.drawingusing system.datausing system.windows.formsnamespace windowsapplication1////// usercontrol1 的摘要说明。
////// //定义委托public delegate void btnokclickeventhanderobject sendereventargs epublic delegate void btncancelclickeventhanderobject sendereventargs epublic class userlogin : system.windows.forms.usercontrol private string usernamepassword 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 system.windows.forms.label label2 private system.windows.forms.label label1 private system.windows.forms.textbox textboxuid /// /// 必需的设计器变量。
/// private system.componentmodel.container components null public userlogin // 该调用是 windows.forms 窗体设计器所必需的。
initializecomponent // todo: 在 initializecomponent 调用后添加任何初始化 /// /// 清理所有正在使用的资源。
/// protected override void dispose bool disposing if disposing ifcomponents null components.dispose base.dispose disposing region 组件设计器生成的代码////// 设计器支持所需的方法 - 不要使用代码编辑器/// 修改此方法的内容。
///private void initializecomponentthis.groupbox1 new system.windows.forms.groupboxthis.buttoncancel new system.windows.forms.buttonthis.buttonok new system.windows.forms.buttonthis.textboxpwd new system.windows.forms.textboxthis.label2 new system.windows.forms.labelthis.label1 new system.windows.forms.labelthis.textboxuid new system.windows.forms.textboxthis.groupbox1.suspendlayoutthis.suspendlayout//// groupbox1//this.groupbox1.controls.addthis.buttoncancelthis.groupbox1.controls.addthis.buttonokthis.groupbox1.controls.addthis.textboxpwdthis.groupbox1.controls.addthis.label2this.groupbox1.controls.addthis.label1this.groupbox1.controls.addthis.textboxuidthis.groupbox1.location new system.drawing.point8 2this.groupbox1.name groupbox1this.groupbox1.size new system.drawing.size200 96this.groupbox1.tabindex 6this.groupbox1.tabstop false//// buttoncancel//this.buttoncancel.locati
上一篇:
ASP_NET多层网络构架下的页间传值方法及应用研究
下一篇:
4种小菜缓解孕妈咪不适