WINFORM similar to the CheckBoxList control in WebForm

zhaozj2021-02-16  57

A few days ago, I saw a post in the .NET technology forum, I seem to remember how to implement the CheckBoxList control in Winform in Winform, I have a simple implementation of such controls.

First, you have to build a control project, if you say:

Then, you add a class: checkboxcollection, it is a collection class for Checkbox

The specific code is as follows

Checkboxcollection.cs

Using system.collections; using system.windows.forms;

namespace CheckListControl {///

/// CheckBox collection class /// 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 global variables

Private checkboxcollection objcbc = new checkboxcollection ();

Public event system.eventhandler checkedchanged;

Write custom functions, external interface

///

/// Add a Checkbox to the control /// /// 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); // Defines the CheckedChanged event to capture its event int y = 0; y = objcbc.count * 24 objcbc.count * 8 12; // forming the longitudinal coordinate of Checkbox CB.Location = New Point 12, Y); Objcbc.Add (CB);

this.controls.add (cb); // Add Checkbox to the control

INT x = getMaxwidth (); // Get the maximum width IF (Cb.Width> x) of the added Checkbox // If the maximum width of the Checkbox added now is greater than the maximum width already added, replace the tune x {x = Cb.Width 24;

this.size = new size (x, y 12 24); / / / / According to the added Checkbox change control size

Return Cb;}

///

/// Automatically forms new adding Checkbox name /// /// private string getName () {if (objcbc.count> 0) {ArrayList List = new arraylist (); for (int i = 0; i 0) {Return "Checkbox" Convert.toString (INT [list.count-1] .tostring (). Trim )) 1);}} Return "CheckBox1";

///

/// Decision is arabic digital /// /// /// private bool isnumber String strs = "0123456789"; foreach (char chr in strway) {if (strcompare == chr.tostring ()) {return true;}} return false;

///

/// Get maximum width ////////////////// private INT getMaxWidth () {INT MAXWIDTH = 0; if (Objcbc.count > 0) {for (int i = 0; i maxwidth) {MaxWidth = Cb.Width;}}} return Maxwidth;}

// [Browsable (TRUE), Description ("Get Checkbox Collection"), Category ("Checklist")] // public checkboxcollection checklist // {// Get // {// Return Objcbc; ///} //}

Private void checkbox_checkedchanged (Object sender, Eventargs E) {CheckedChanged (Sender, New Eventargs ());} You can get CheckListControl.dll after compiling;

Add new projects for class test

In the toolbox -> Add / remove the item, add checklistControl, then drag CHECKLISTCONTROL to FORM1

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);

This is the case

In fact, just a simple CheckBoxList, there are many other features, and hope users correct, add more features.

转载请注明原文地址:https://www.9cbs.com/read-22352.html

New Post(0)