在.net應(yīng)用程序中使用用戶控件
2024-07-10 13:03:51
供稿:網(wǎng)友
注冊(cè)會(huì)員,創(chuàng)建你的web開發(fā)資料庫(kù),在.net應(yīng)用程序中使用用戶控件
鄭佐2004-11-30
做過(guò)asp.net的人都知道開發(fā)的時(shí)候使用用戶控件很方便,為功能模塊化提供了相當(dāng)大的靈活性。令人高興的是開發(fā)windows窗體也可以使用用戶控件。這里我們來(lái)看看為用戶控件添加屬性和事件,并實(shí)現(xiàn)把消息發(fā)送到父容器。本文主要是為沒(méi)有使用過(guò)用戶控件的朋友提供一些參考。
用戶控件的實(shí)現(xiàn)比較簡(jiǎn)單,直接從system.windows.forms.usercontrol。
public class usercontrol1 : system.windows.forms.usercontrol
為了便于測(cè)試我在上面添加了一個(gè)textbox,并注冊(cè)textbox的textchanged事件,
this.textbox1.textchanged += new system.eventhandler(this.textbox1_textchanged);
事件處理函數(shù),
private void textbox1_textchanged(object sender, system.eventargs e)
{
messagebox.show(this.textbox1.text);
}
這里演示如果控件中文本框的內(nèi)容改變就會(huì)用messagebox顯示當(dāng)前的文本框內(nèi)容。
控件顯示如下:
在窗體中添加上面的用戶控件,當(dāng)我們改變textbox的文本時(shí),可以看到跳出一個(gè)對(duì)話框,很簡(jiǎn)單吧。
下面來(lái)看看對(duì)控件添加屬性。
這里定義一個(gè)私有變量。
private string customvalue;
添加訪問(wèn)他的屬性
public string customvalue
{
get{return customvalue;}
set{customvalue =value;}
}
在窗體中使用的時(shí)候像普通控件一樣進(jìn)行訪問(wèn),
usercontrol11.customvalue = "用戶控件自定義數(shù)據(jù)";
通過(guò)事件可以傳遞消息到窗體上,在定義之前我們先來(lái)寫一個(gè)簡(jiǎn)單的參數(shù)類。
public class textchangeeventargs : eventargs
{
private string message;
public textchangeeventargs(string message)
{
this.message = message;
}
public string message
{
get{return message;}
}
}
定義委托為,
public delegate void textboxchangedhandle(object sender,textchangeeventargs e);
接下去在用戶控件中添加事件,
//定義事件
public event textboxchangedhandle usercontrolvaluechanged;
為了激發(fā)用戶控件的新增事件,修改了一下代碼,
private void textbox1_textchanged(object sender, system.eventargs e)
{
if(usercontrolvaluechanged != null)
usercontrolvaluechanged(this,new textchangeeventargs(this.textbox1.text));
}
好了,為了便于在csdn上回答問(wèn)題,把完整的代碼貼了出來(lái):
using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.data;
using system.windows.forms;
namespace zz.windowsapplication1
{
public class usercontrol1 : system.windows.forms.usercontrol
{
private system.windows.forms.textbox textbox1;
private string customvalue;
private system.componentmodel.container components = null;
public string customvalue
{
get{return customvalue;}
set{customvalue =value;}
}
//定義事件
public event textboxchangedhandle usercontrolvaluechanged;
public usercontrol1()
{
initializecomponent();
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region 組件設(shè)計(jì)器生成的代碼
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.suspendlayout();
this.textbox1.location = new system.drawing.point(12, 36);
this.textbox1.name = "textbox1";
this.textbox1.tabindex = 0;
this.textbox1.text = "textbox1";
this.textbox1.textchanged += new system.eventhandler(this.textbox1_textchanged);
this.controls.add(this.textbox1);
this.name = "usercontrol1";
this.size = new system.drawing.size(150, 92);
this.resumelayout(false);
}
#endregion
private void textbox1_textchanged(object sender, system.eventargs e)
{
if(usercontrolvaluechanged != null)
usercontrolvaluechanged(this,new textchangeeventargs(this.textbox1.text));
}
}
//定義委托
public delegate void textboxchangedhandle(object sender,textchangeeventargs e);
public class textchangeeventargs : eventargs
{
private string message;
public textchangeeventargs(string message)
{
this.message = message;
}
public string message
{
get{return message;}
}
}
}
使用時(shí)要在窗體中注冊(cè)上面的事件,比較簡(jiǎn)單都貼源代碼了,
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace zz.windowsapplication1
{
public class form1 : system.windows.forms.form
{
private windowsapplication1.usercontrol1 usercontrol11;
private system.componentmodel.container components = null;
public form1()
{
initializecomponent();
usercontrol11.customvalue = "用戶控件自定義數(shù)據(jù)";
usercontrol11.usercontrolvaluechanged += new textboxchangedhandle(usercontrol11_usercontrolvaluechanged);
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設(shè)計(jì)器生成的代碼
private void initializecomponent()
{
this.usercontrol11 = new windowsapplication1.usercontrol1();
this.suspendlayout();
this.usercontrol11.location = new system.drawing.point(8, 8);
this.usercontrol11.name = "usercontrol11";
this.usercontrol11.size = new system.drawing.size(150, 84);
this.usercontrol11.tabindex = 0;
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(292, 193);
this.controls.add(this.usercontrol11);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
[stathread]
static void main()
{
application.run(new form1());
}
private void usercontrol11_usercontrolvaluechanged(object sender, textchangeeventargs e)
{
messagebox.show("當(dāng)前控件的值為:" + e.message);
}
}
}
另外需要?jiǎng)討B(tài)加載,就把控件添加在容器的controls集合就行了,下面是在構(gòu)造函數(shù)中添加控件,
public form1()
{
initializecomponent();
usercontrol1 uc = new usercontrol1();
uc.customvalue = "動(dòng)態(tài)加載的用戶控件";
uc.usercontrolvaluechanged += new textboxchangedhandle(usercontrol11_usercontrolvaluechanged);
this.controls.add(uc);
}
另外從vs.net中的工具箱中拖動(dòng)用戶控件到窗體上,如果是第一次需要編譯一下項(xiàng)目。