TreeView for WinForms
チェックリスト

CheckList control displays a collection of items via list box embedded with multiple check boxes (small square shaped selection boxes). It enables multiple selection of items using different states of the check boxes. Additionally, the CheckList control is represented by the C1CheckList class which allows you to perform various operations on the control.

Add and Remove CheckList Items

You can access the item collection of CheckList using Items property of the C1CheckList class and add or remove the objects from the list at runtime. To add an item to the CheckList, you can use the Add method.

This image shows example of CheckList control.

The following sample code add items from the CheckList control.

Add CheckList Items
コードのコピー
// すべてのアイテムをCheckListコントロールに追加します
lCheckList.Items.Add(c1CheckListItem1);
lCheckList.Items.Add(c1CheckListItem2);
lCheckList.Items.Add(c1CheckListItem3);
lCheckList.Items.Add(c1CheckListItem4);
lCheckList.Items.Add(c1CheckListItem5);
lCheckList.Items.Add(c1CheckListItem6);
lCheckList.Items.Add(c1CheckListItem7);
// 各チェックリスト項目に名前を割り当てます
c1CheckListItem1.Value = "Violet";
c1CheckListItem2.Value = "Indigo";
c1CheckListItem3.Value = "Blue";
c1CheckListItem4.Value = "Green";
c1CheckListItem5.Value = "Yellow";
c1CheckListItem6.Value = "Orange";
c1CheckListItem7.Value = "Red";

To remove specific items from the list, you can use Remove method. The following sample code remove items from the CheckList control.

Remove CheckList Items
コードのコピー
lCheckList.Items.Remove(c1CheckListItem5);
lCheckList.Items.Remove(c1CheckListItem7);

Data Binding

Binding a control with a data source gives you ability to communicate and even update the underlying data through the control. You can bind CheckList using any of the following ways:

Bind with Database

You can bind the CheckList to a data source by following these steps:

  1. Create a connection string and fetch data from a database to a data set.
    C#
    コードのコピー
    static string GetConnectionString()
            {
                string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Common\C1NWind.mdb";
                return string.Format(conn);
            }
            DataTable GetDataSource(string connectionString)
            {
                // 接続文字列を設定します
                string conn = GetConnectionString();
                // SQL文を設定します
                string rs = connectionString;
                // DataSetにデータを取得します
                OleDbDataAdapter da = new OleDbDataAdapter(rs, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                // データテーブルを返します
                return ds.Tables[0];
            }
    
  2. Set the DataSource and DisplayMemberPath properties of the CheckList control.
    C#
    コードのコピー
    lCheckList.BindingInfo.DataSource = GetDataSource("Select * from Employees ");
    lCheckList.BindingInfo.DisplayMemberPath = "FirstName";
    

After completing the above mentioned steps, you will get the following result.

Bind DataSource with CheckList

Bind with Object Collection

To bind the CheckList control to a list of data objects, follow these steps:

  1. Create a class named Customer using the following code.
    C#
    コードのコピー
    public class Customer
    {
        string name;
        string customerID;
        string mobile;
        string email;
        public Customer(string _name, string _custId, string _mobile, string _email)
        {
            this.name = _name;
            this.customerID = _custId;
            this.mobile = _mobile;
            this.email = _email;
        }
        public string Name
        {
            get
            {
                return name;
            }
        }
        public string CustomerID
        {
            get
            {
                return customerID;
            }
        }
        public string Mobile
        {
            get
            {
                return mobile;
            }
        }
        public string Email
        {
            get
            {
                return email;
            }
        }
    }
    
  2. Add objects of Customer class to a BindingList and set C1CheckList’s DataSource and DisplayMemberPath properties at Form_Load event.
    C#
    コードのコピー
    BindingList<Customer> customers = new BindingList<Customer>();
    customers.Add(new Customer("John", "C001", "8888832141", "john@gmail.com"));
    customers.Add(new Customer("Alex", "C002", "8888832142", "@gmail.com"));
    customers.Add(new Customer("Shawn", "C003", "8888832143", "shawn@gmail.com"));
    customers.Add(new Customer("Sam", "C004", "8888832144", "sam@gmail.com"));
    customers.Add(new Customer("Neo", "C005", "8888832145", "neo@gmail.com"));
    customers.Add(new Customer("Paul", "C006", "8888832146", "paul@gmail.com"));
    
    lCheckList.BindingInfo.DataSource = customers;
    lCheckList.BindingInfo.DisplayMemberPath = "Name";
    

After completing the above mentioned steps, you get the following result.

Bind Object Collection with CheckList

Selection

You can select/deselect all items in the list with single selection. To enable this option, you need to set the ShowSelectAll property to true. On setting this property to true, the Select All check box appears on top of the list of items. When you deselect the Select All check box, all items in the list are deselected.

Set ShowSelectAll property in CheckList

The following sample code shows how to select/deselect all items in the list.

C#
コードのコピー
// [すべて選択]チェックボックスを表示します
lCheckList.ShowSelectAll = true;

In order to display CheckList control with the selected item at initial stage, you can use the following sample code.

Set item in the initial stage

C#
コードのコピー
// 上から3番目の項目を、チェックリストコントロールで選択した状態で表示します
lCheckList.SelectedItem = c1CheckListItem3;

You can use the SelectionMode property to determine whether a user can select one or more than one item from the header. This property lets you choose between the following selection modes through SelectionMode enumeration:

Select multiple values in CheckList

The following sample code shows the method to set the SelectionMode property.

C#
コードのコピー
// 複数のアイテムを同時に選択します
lCheckList.SelectionMode = C1.Win.TreeView.SelectionMode.Multiple;

Show or Hide CheckBoxes

You can set the visibility of check boxes in the CheckList control by using ShowCheckBoxes property of the C1CheckList class. The following image displays CheckList items without the check boxes.

Hide checkboxes in CheckList

The following sample code sets the visibility of checkbox in the CheckList control.

C#
コードのコピー
// 各チェックリスト項目のチェックボックスを非表示にします
lCheckList.ShowCheckBoxes = false;

Customize Captions

You can customize the CheckList control as per your requirements. You can change the captions of Select All option using SelectAllCaption property and the caption of Unselect All option using UnselectAllCaption property of the C1CheckList class.

Set captions for Select All/ Unselect All option

The following sample code sets the SelectAllCaption and UnselectAllCaption property of CheckList.

C#
コードのコピー
lCheckList.ShowCheckBoxes = true;
lCheckList.SelectAllCaption = "Colors Check All";
lCheckList.UnselectAllCaption = "Colors UnCheck All";

Appearance and Style

CheckList allows you to change the appearance and style of all items in the control. You can use the BorderColor and BorderStyle property to style the border of the CheckList. In addition, you can also set the style of default, selected and hot state of CheckList  control. The following GIF shows the style applied to the CheckList's items.

Change style of CheckList

C#
コードのコピー
// チェックボックスの色を設定します
lCheckList.Styles.CheckBoxColor = Color.Brown;
// 境界線のスタイルを設定します
lCheckList.BorderStyle = BorderStyle.Fixed3D;
lCheckList.BorderColor = Color.Black;
// チェックリストのデフォルト状態のスタイルを取得します
lCheckList.Styles.Default.BackColor = Color.OldLace;
lCheckList.Styles.Default.ForeColor = Color.Maroon;
// チェックリストの選択された状態のスタイルを取得します
lCheckList.Styles.Selected.BackColor = Color.LightYellow;
lCheckList.Styles.Selected.ForeColor = Color.DeepPink;
// チェックリストのホット状態のスタイルを取得します
lCheckList.Styles.Hot.BackColor = Color.Orange;
lCheckList.Styles.Hot.ForeColor = Color.LightGray;