前些天,在.Net技术的论坛里面看到了有个帖子,我好像记得是怎么实现WinForm中类似WebForm中的CheckBoxList控件,我简单的实现了那样的一个控件
首先,你得建立一个控件项目,假如说是: 
接着,你就添加一个类:CheckBoxCollection,它是个CheckBox的集合类
具体的代码如下
CheckBoxCollection.cs
using System;
using System.Collections;
using System.Windows.Forms;
namespace CheckListControl
{
///
1<summary>
2/// CheckBox的集合类
3/// </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;
写自定义函数,外部接口
///
1<summary>
2/// 新增一个CheckBox到控件
3/// </summary>
///
1<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的纵坐标
cb.Location=new Point(12,y);
objCbc.Add(cb);
this.Controls.Add(cb);//添加CheckBox到控件
int x=GetMaxWidth();//得到已经添加的CheckBox中的最大的宽度
if(cb.Width >x )//如果现在添加的CheckBox的最大宽度大于已经添加的最大宽度,替换调x
{
x = cb.Width + 24;
}
this.Size=new Size(x ,y +12+24);//根据添加的CheckBox改变控件的大小
return cb;
}
///
1<summary>
2/// 自动形成新添加CheckBox的名称
3/// </summary>
///
1<returns></returns>
private string GetName()
{
if(objCbc.Count>0)
{
ArrayList list=new ArrayList();
for(int i=0;i
1<objcbc.count;i++) &&="" if(list.count="" if(objcbc[i].name.trim().length="9)" if(str.substring(0,8).tolower()="checkbox" isnumber(str.substring(str.length-1,1)))="" list.add(str.substring(str.length-1,1));="" str="objCbc[i].Name.Trim();" string="" {="" }="">0)
2{
3return "checkBox" + Convert.ToString(int.Parse(list[list.Count-1].ToString().Trim()) + 1);
4}
5}
6
7return "checkBox1";
8}
9
10/// <summary>
11/// 判断是否是阿拉伯数字
12/// </summary>
13/// <param name="strCompare"/>
14/// <returns></returns>
15private bool IsNumber(string strCompare)
16{
17string strWord="0123456789";
18foreach(char chr in strWord)
19{
20if(strCompare==chr.ToString())
21{
22return true;
23}
24}
25return false;
26}
27
28/// <summary>
29/// 得到已经添加CheckBox中的最大宽度
30/// </summary>
31/// <returns></returns>
32private int GetMaxWidth()
33{
34int maxWidth=0;
35if(objCbc.Count>0)
36{
37for(int i=0;i<objcbc.count;i++) cb="(CheckBox)objCbc[i];" checkbox="" if(cb.width="" {="">maxWidth)
38{
39maxWidth=cb.Width;
40}
41}
42}
43return maxWidth;
44}
45
46// [Browsable(true), Description("得到CheckBox集合"), Category("CheckList")]
47// public CheckBoxCollection CheckList
48// {
49// get
50// {
51// return objCbc;
52// }
53// }
54
55private void CheckBox_CheckedChanged(object sender, EventArgs e)
56{
57CheckedChanged(sender,new EventArgs());
58}
59
60编译以后,就可以得到CheckListControl.dll;
61
62添加新项目用于类的测试
63
64
65
66在工具箱中->添加/移除项,把CheckListControl添加进来,然后拖CheckListControl到Form1中
67
68form1.cs中
69
70private void Form1_Load(object sender, System.EventArgs e)
71{
72CheckBox cb=checkBoxList1.NewCheckBox();
73cb.Name="chkFirst";
74cb.Text="第一个CheckBox";
75cb.Size=new Size(125,24);
76cb=checkBoxList1.NewCheckBox();
77cb.Name="chkSecond";
78cb.Text="第二个CheckBox";
79cb.Size=new Size(125,24);
80}
81
82private void checkBoxList1_CheckedChanged(object sender, System.EventArgs e)
83{
84CheckBox cb=(CheckBox)sender;
85MessageBox.Show("Name: " + cb.Name + " Text: " +cb.Text);
86}
87
88具体的就这样
89
90其实,只是作了简单的一个CheckBoxList,具体很多还有其它的功能没有加上,希望网友指正,添加更多功能</objcbc.count;i++)></objcbc.count;i++)>