CheckBoxList controls in WinForm similar to WebForm

Source: Internet
Author: User
Tags define chr tostring trim
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;
}

public void Remove (CheckBox obj)
{
Base. List.remove (obj);
}
}
}


Then, in the CheckBoxList.cs file, define the global variable

Private checkboxcollection objcbc=new checkboxcollection ();

public event System.EventHandler CheckedChanged;

Write a custom function, external interface



<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&GT;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;
}

[Browsable (True), Description ("Get checkbox Set"), Category ("Checklist")]
Public Checkboxcollection Checklist
// {
Get
// {
return OBJCBC;
// }
// }

private void CheckBox_CheckedChanged (object sender, EventArgs e)
{
CheckedChanged (Sender,new EventArgs ());
}



After compiling, we can get CheckListControl.dll;

To add a new project to a test for a class



-> Add/Remove items in the toolbox, add Checklistcontrol, and drag Checklistcontrol to Form1

In Form1.cs

private void Form1_Load (object sender, System.EventArgs e)
{
CheckBox Cb=checkboxlist1.newcheckbox ();
Cb. Name= "Chkfirst";
Cb. text= "First checkbox";
Cb. Size=new Size (125,24);
Cb=checkboxlist1.newcheckbox ();
Cb. Name= "Chksecond";
Cb. text= "Second 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);
}

That's exactly what it is.

In fact, just made a simple checkboxlist, a lot of other functions did not add, I hope users to correct, add more features


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.