前些天,在.net技術(shù)的論壇里面看到了有個(gè)帖子,我好像記得是怎么實(shí)現(xiàn)winform中類似webform中的checkboxlist控件,我簡(jiǎn)單的實(shí)現(xiàn)了那樣的一個(gè)控件
首先,你得建立一個(gè)控件項(xiàng)目,假如說是:
接著,你就添加一個(gè)類:checkboxcollection,它是個(gè)checkbox的集合類
具體的代碼如下
checkboxcollection.cs
using system;
using system.collections;
using system.windows.forms;
namespace checklistcontrol
{
/// <summary>
/// checkbox的集合類
/// </summary>
public class checkboxcollection:system.collections.collectionbase
{
public checkboxcollection()
{
ilist pilist=base.list;
}
public checkbox this[int index]
{
get
{
return (checkbox) list[index];
}
}
public checkbox add(checkbox obj)
{
base.list.add(obj);
return obj;
}
public void remove(checkbox obj)
{
base.list.remove(obj);
}
}
}
然后,在checkboxlist.cs文件中,定義全局變量
private checkboxcollection objcbc=new checkboxcollection();
public event system.eventhandler checkedchanged;
寫自定義函數(shù),外部接口
/// <summary>
/// 新增一個(gè)checkbox到控件
/// </summary>
/// <returns></returns>
public checkbox newcheckbox()
{
lab.visible=false;
checkbox cb=new checkbox();
cb.name=getname();
cb.text=cb.name;
// cb.size=new size(120,24);
cb.checked=false;
cb.visible=true;
cb.checkedchanged+=new eventhandler(checkbox_checkedchanged);//定義checkedchanged事件,來捕捉它的事件
int y=0;
y=objcbc.count * 24 + objcbc.count * 8 + 12;//形成checkbox的縱坐標(biāo)
cb.location=new point(12,y);
objcbc.add(cb);
this.controls.add(cb);//添加checkbox到控件
int x=getmaxwidth();//得到已經(jīng)添加的checkbox中的最大的寬度
if(cb.width >x )//如果現(xiàn)在添加的checkbox的最大寬度大于已經(jīng)添加的最大寬度,替換調(diào)x
{
x = cb.width + 24;
}
this.size=new size(x ,y +12+24);//根據(jù)添加的checkbox改變控件的大小
return cb;
}
/// <summary>
/// 自動(dòng)形成新添加checkbox的名稱
/// </summary>
/// <returns></returns>
private string getname()
{
if(objcbc.count>0)
{
arraylist list=new arraylist();
for(int i=0;i<objcbc.count;i++)
{
if(objcbc[i].name.trim().length==9)
{
string str=objcbc[i].name.trim();
if(str.substring(0,8).tolower()=="checkbox" && isnumber(str.substring(str.length-1,1)))
{
list.add(str.substring(str.length-1,1));
}
}
}
if(list.count>0)
{
return "checkbox" + convert.tostring(int.parse(list[list.count-1].tostring().trim()) + 1);
}
}
return "checkbox1";
}
/// <summary>
/// 判斷是否是阿拉伯?dāng)?shù)字
/// </summary>
/// <param name="strcompare"></param>
/// <returns></returns>
private bool isnumber(string strcompare)
{
string strword="0123456789";
foreach(char chr in strword)
{
if(strcompare==chr.tostring())
{
return true;
}
}
return false;
}
/// <summary>
/// 得到已經(jīng)添加checkbox中的最大寬度
/// </summary>
/// <returns></returns>
private int getmaxwidth()
{
int maxwidth=0;
if(objcbc.count>0)
{
for(int i=0;i<objcbc.count;i++)
{
checkbox cb=(checkbox)objcbc[i];
if(cb.width>maxwidth)
{
maxwidth=cb.width;
}
}
}
return maxwidth;
}
// [browsable(true), description("得到checkbox集合"), category("checklist")]
// public checkboxcollection checklist
// {
// get
// {
// return objcbc;
// }
// }
private void checkbox_checkedchanged(object sender, eventargs e)
{
checkedchanged(sender,new eventargs());
}
編譯以后,就可以得到checklistcontrol.dll;
添加新項(xiàng)目用于類的測(cè)試
在工具箱中->添加/移除項(xiàng),把checklistcontrol添加進(jìn)來,然后拖checklistcontrol到form1中
form1.cs中
private void form1_load(object sender, system.eventargs e)
{
checkbox cb=checkboxlist1.newcheckbox();
cb.name="chkfirst";
cb.text="第一個(gè)checkbox";
cb.size=new size(125,24);
cb=checkboxlist1.newcheckbox();
cb.name="chksecond";
cb.text="第二個(gè)checkbox";
cb.size=new size(125,24);
}
private void checkboxlist1_checkedchanged(object sender, system.eventargs e)
{
checkbox cb=(checkbox)sender;
messagebox.show("name: " + cb.name + " text: " +cb.text);
}
具體的就這樣
其實(shí),只是作了簡(jiǎn)單的一個(gè)checkboxlist,具體很多還有其它的功能沒有加上,希望網(wǎng)友指正,添加更多功能