如果在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 btncancelclick
6、事件實現
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
{
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.panel panel1;
private windowsapplication1.userlogin userlogin1;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private system.componentmodel.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();
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 /// <summary>
/// 應用程式的主進入點。
/// </summary>
[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
{
/// <summary>
/// usercontrol1 的摘要說明。
/// </summary>
/// //定義委託
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;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private system.componentmodel.container components = null; public userlogin()
{
// 該調用是 windows.forms 表單設計器所必需的。
initializecomponent(); // todo: 在 initializecomponent 調用後添加任何初始化 } /// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
} #region 組件設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內容。
/// </summary>
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);
}
}
}