web| control a few days ago, in the. NET Technology Forum saw a post, I seem to remember how to implement the WinForm in a similar webform in the CheckBoxList control, I simply implemented a control like that
First, you have to build a control project, if you say:
Next, you add a class: Checkboxcollection, which is the collection class for a checkbox
The specific code is as follows
CheckBoxCollection.cs
Using System;
Using System.Collections;
Using System.Windows.Forms;
Namespace Checklistcontrol
{
<summary>
Collection Class for 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;
}
<summary>
Add a checkbox to a control
</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);//define CheckedChanged event to catch its event
int y=0;
Y=objcbc.count * + objcbc.count * 8 + 12;//to form the ordinate of the checkbox
Cb. Location=new Point (12,y);
Objcbc.add (CB);
This. Controls.Add (CB);//Add checkbox to control
int x=getmaxwidth ()///Get the maximum width of the checkbox that has been added
if (CB). Width >x)//If the maximum width of the checkbox added now is greater than the maximum width that has been added, replace the X
{
x = cb. Width + 24;
}
This. Size=new size (x, y +12+24);//change the size of the control based on the added checkbox
return CB;
}
<summary>
Automatically form the name of the newly added 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>
Judge whether it is an Arabic numeral
</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>
Get the maximum width of the checkbox that has been added
</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;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.