如果在c#中实现用户定义控件内的事件挂勾到调用的窗体事件中
我们都知道在asp.net开发中,如果使用用户定义控件可以有效的进行程序的模块化。其实在.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:"+userlogin1.username+";pwd:"+userlogin1.password,"usermessage");
}protected void cancelclick(object send,system.eventargs e)
{
this.close();
}8.按f5运行(如图result)附1(windowsapplication1源代码)using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;namespace 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 窗体设计器支持所必需的
//
initializecomponent();
userlogin1.btnokclick+=new btnokclickeventhander(okclick);
userlogin1.btncancelclick+=new btncancelclickeventhander(cancelclick);
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}///
/// 清理所有正在使用的资源。
///
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}#region windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void initializecomponent()
{
this.panel1 = new system.windows.forms.panel();
this.userlogin1 = new windowsapplication1.userlogin();
this.panel1.suspendlayout();
this.suspendlayout();
//
// panel1
//
this.panel1.controls.add(this.userlogin1);
this.panel1.dock = system.windows.forms.dockstyle.fill;
this.panel1.location = new system.drawing.point(0, 0);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(216, 101);
this.panel1.tabindex = 1;
//
// userlogin1
//
this.userlogin1.dock = system.windows.forms.dockstyle.fill;
this.userlogin1.location = new system.drawing.point(0, 0);
this.userlogin1.name = "userlogin1";
this.userlogin1.size = new system.drawing.size(216, 101);
this.userlogin1.tabindex = 0;
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(216, 101);
this.controls.add(this.panel1);
this.name = "form1";
this.text = "form1";
this.panel1.resumelayout(false);
this.resumelayout(false);}
#endregion///
/// 应用程序的主入口点。
///
[stathread]
static void main()
{
application.run(new form1());
}
protected void okclick(object send,system.eventargs e)
{
messagebox.show("uid:"+userlogin1.username+";pwd:"+userlogin1.password,"usermessage");
}protected void cancelclick(object send,system.eventargs e)
{
this.close();
}}
}
附2(userlogin源代码)using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.data;
using system.windows.forms;namespace windowsapplication1
{
///
/// usercontrol1 的摘要说明。
///
/// //定义委托
public delegate void btnokclickeventhander(object sender,eventargs e);
public delegate void btncancelclickeventhander(object sender,eventargs e);
public class userlogin : system.windows.forms.usercontrol
{
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 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 )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
///
private void initializecomponent()
{
this.groupbox1 = new system.windows.forms.groupbox();
this.buttoncancel = new system.windows.forms.button();
this.buttonok = new system.windows.forms.button();
this.textboxpwd = new system.windows.forms.textbox();
this.label2 = new system.windows.forms.label();
this.label1 = new system.windows.forms.label();
this.textboxuid = new system.windows.forms.textbox();
this.groupbox1.suspendlayout();
this.suspendlayout();
//
// groupbox1
//
this.groupbox1.controls.add(this.buttoncancel);
this.groupbox1.controls.add(this.buttonok);
this.groupbox1.controls.add(this.textboxpwd);
this.groupbox1.controls.add(this.label2);
this.groupbox1.controls.add(this.label1);
this.groupbox1.controls.add(this.textboxuid);
this.groupbox1.location = new system.drawing.point(8, 2);
this.groupbox1.name = "groupbox1";
this.groupbox1.size = new system.drawing.size(200, 96);
this.groupbox1.tabindex = 6;
this.groupbox1.tabstop = false;
//
// buttoncancel
//
this.buttoncancel.location = new system.drawing.point(112, 71);
this.buttoncancel.name = "buttoncancel";
this.buttoncancel.size = new system.drawing.size(56, 20);
this.buttoncancel.tabindex = 11;
this.buttoncancel.text = "&cancel";
this.buttoncancel.click += new system.eventhandler(this.buttoncancel_click);
//
// buttonok
//
this.buttonok.location = new system.drawing.point(32, 71);
this.buttonok.name = "buttonok";
this.buttonok.size = new system.drawing.size(56, 20);
this.buttonok.tabindex = 10;
this.buttonok.text = "&ok";
this.buttonok.click += new system.eventhandler(this.buttonok_click);
//
// textboxpwd
//
this.textboxpwd.location = new system.drawing.point(72, 43);
this.textboxpwd.name = "textboxpwd";
this.textboxpwd.passwordchar = *;
this.textboxpwd.size = new system.drawing.size(112, 21);
this.textboxpwd.tabindex = 9;
this.textboxpwd.text = "";
this.textboxpwd.textchanged += new system.eventhandler(this.textboxpwd_textchanged);
//
// label2
//
this.label2.location = new system.drawing.point(9, 42);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(64, 23);
this.label2.tabindex = 8;
this.label2.text = "password:";
this.label2.textalign = system.drawing.contentalignment.middlecenter;
//
// label1
//
this.label1.location = new system.drawing.point(9, 14);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(64, 23);
this.label1.tabindex = 7;
this.label1.text = "username:";
this.label1.textalign = system.drawing.contentalignment.middlecenter;
//
// textboxuid
//
this.textboxuid.location = new system.drawing.point(72, 15);
this.textboxuid.name = "textboxuid";
this.textboxuid.size = new system.drawing.size(112, 21);
this.textboxuid.tabindex = 6;
this.textboxuid.text = "";
this.textboxuid.textchanged += new system.eventhandler(this.textboxuid_textchanged);
//
// userlogin
//
this.controls.add(this.groupbox1);
this.name = "userlogin";
this.size = new system.drawing.size(216, 104);
this.groupbox1.resumelayout(false);
this.resumelayout(false);}
#endregion
//定义属性
public string username
{
get{return username;}
set{username=value;}
}
public string password
{
get{return password;}
set{password=value;}
}
//定义事件
public event btnokclickeventhander btnokclick;
public event btncancelclickeventhander btncancelclick;
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);
}
}
}