前些天,在.Net技術的論壇里面看到了有個帖子,我好像記得是怎么實現WinForm中類似WebForm中的CheckBoxList控件,我簡單的實現了那樣的一個控件
首先,你得建立一個控件項目,假如說是:
接著,你就添加一個類:CheckBoxCollection,它是個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;
寫自定義函數,外部接口
/// <summary> /// 新增一個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的縱坐標 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; }
/// <summary> /// 自動形成新添加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> /// 判斷是否是阿拉伯數字 /// </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> /// 得到已經添加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;
添加新項目用于類的測試
在工具箱中->添加/移除項,把CheckListControl添加進來,然后拖CheckListControl到Form1中
form1.cs中
private void Form1_Load(object sender, System.EventArgs e) { CheckBox cb=checkBoxList1.NewCheckBox(); cb.Name="chkFirst"; cb.Text="第一個CheckBox"; cb.Size=new Size(125,24); cb=checkBoxList1.NewCheckBox(); cb.Name="chkSecond"; cb.Text="第二個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); }
具體的就這樣
其實,只是作了簡單的一個CheckBoxList,具體很多還有其它的功能沒有加上,希望網友指正,添加更多功能
|