C# Windows Forms CheckedListBox Example

Learn how to use the CheckedListBox control in C# Windows Forms

This tutorial will help you learn the following concepts:

  1. How to create a windows forms with a CheckedListBox.
  2. How to programmatically add items to a CheckedListBox.
  3. How to get the selected item in a CheckedListBox.
  4. How to handle ItemCheck event in CheckedListBox

Example 1: SimpleCheckedListBox Example

This example will comprise the following files:

  • CheckedListBox.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). CheckedListBox.cs

Create a file named CheckedListBox.cs

Add these using statements:

using System;
using System.Windows.Forms;

Then create our own namespace:

namespace Examples {

Extend the Form class to create our MainForm

  class MainForm : Form {

Create a CheckedListBox:

    private CheckedListBox checkedlistBox = new CheckedListBox();

In our Main()function is where we'll run our app:

    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

Create our MainForm() constructor and assigne the default values of our MainForm form:

    public MainForm() {
      Text = "CheckedListBox example";
      StartPosition = FormStartPosition.Manual;
      Location = new System.Drawing.Point(200, 200);
      ClientSize = new System.Drawing.Size(200, 240);

Add our CheckedListBox to our Form:

      checkedlistBox.Parent = this;

Set the CheckedListBox anchor:

      checkedlistBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
  checkedlistBox.Bounds = new System.Drawing.Rectangle(20, 20, 160, 200);

 Loop for one to ten:

```csharp
      for (int i = 1; i <= 10; ++i)

For each iteration add the current item to our CheckedListBox:

        checkedlistBox.Items.Add(string.Format("Item {0}", i), i % 2 != 0);

Handle the ItemCheck property of each added CheckedListBox item:

      checkedlistBox.ItemCheck += delegate (object sender, ItemCheckEventArgs e) {
        System.Diagnostics.Debug.WriteLine(string.Format("item check, Index ={0}, NewValue={1}, CurrentValue={2}", e.Index, e.NewValue, e.CurrentValue));
      };
    }

Here is the full code


using System;
using System.Windows.Forms;
namespace Examples {
  class MainForm : Form {
    private CheckedListBox checkedlistBox = new CheckedListBox();
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "CheckedListBox example";
      StartPosition = FormStartPosition.Manual;
      Location = new System.Drawing.Point(200, 200);
      ClientSize = new System.Drawing.Size(200, 240);
      checkedlistBox.Parent = this;
      checkedlistBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
      checkedlistBox.Bounds = new System.Drawing.Rectangle(20, 20, 160, 200);
      for (int i = 1; i <= 10; ++i)
        checkedlistBox.Items.Add(string.Format("Item {0}", i), i % 2 != 0);
      checkedlistBox.ItemCheck += delegate (object sender, ItemCheckEventArgs e) {
        System.Diagnostics.Debug.WriteLine(string.Format("item check, Index ={0}, NewValue={1}, CurrentValue={2}", e.Index, e.NewValue, e.CurrentValue));
      };
    }

  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author