New to Telerik UI for WinForms? Download free 30-day trial

Getting Started with WinForms CheckedListBox

You can add RadCheckedListBox either at design time or at run time:

Design Time

  1. To add a RadCheckedListBox to your form, drag a RadCheckedListBox from the toolbox onto the surface of the form designer.
  2. In the Properties section in Visual Studio open the Items property.
  3. Add several items by clicking the Add button.
  4. Click F5 to start the application.

Run Time

To programmatically add a RadCheckedListBox to a form, create a new instance of a RadCheckedListBox, and add it to the form Controls collection.

Adding a RadCheckedListBox at runtime

RadCheckedListBox checkedListBox = new RadCheckedListBox();
this.Controls.Add(checkedListBox);
checkedListBox.Items.Add("Coffee");
checkedListBox.Items.Add("Tea");

The bellow example demonstrates the main capabilities of RadCheckedListBox.

1. Drop a RadSplitContainer on your form and set its Dock property to Fill .

2. Add two panels to the split container. For example by using the smart tag.

3. Add RadLabel, RadCheckedListBox and a RadButton to each of the panels. At this point the form should look like this: WinForms RadCheckedListBox checkedlistbox-getting-started 001

4. Now you are ready to bind the control. Open the code behind and add the following:

this.radCheckedListBox1.DataSource = this.CreatePhoneBookEntries();
this.radCheckedListBox1.VisualItemFormatting += radCheckedListBox1_VisualItemFormatting;
ListViewDetailColumn nameColumn = new ListViewDetailColumn("Name");
nameColumn.Width = 150;
this.radCheckedListBox2.Columns.Add(nameColumn);
ListViewDetailColumn phoneColumn = new ListViewDetailColumn("Phone");
phoneColumn.Width = 100;
this.radCheckedListBox2.Columns.Add(phoneColumn);
this.radButtonAddToContacts.Click += radButtonAddToContacts_Click;
this.radButtonRemoveFromContacts.Click += radButtonRemoveFromContacts_Click;

5. The example uses the following sample business object:

public class PhonebookEntry
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
    public string Address { get; set; }
    public Image Image { get; set; }
}

6. Now you can create a collection of PhonebookEntry business objects:

private IEnumerable<PhonebookEntry> CreatePhoneBookEntries()
{
    List<PhonebookEntry> entries = new List<PhonebookEntry>() 
    {
        new PhonebookEntry() { FirstName = "Anne", LastName = "Dodsworth", PhoneNumber = "(71) 555-4444", Address = "7 Houndstooth Rd.", Image = Resources.anne},
        new PhonebookEntry() { FirstName = "Laura", LastName = "Callahan", PhoneNumber = "(206) 555-1189", Address = "4726 - 11th Ave. N.E.", Image = Resources.laura },
        new PhonebookEntry() { FirstName = "Robert", LastName = "King", PhoneNumber = "(71) 555-5598", Address = "Edgeham Hollow Winchester Way", Image = Resources.robert },
        new PhonebookEntry() { FirstName = "Michael", LastName = "Suyama", PhoneNumber = "(71) 555-7773", Address = "Coventry House Miner Rd.", Image = Resources.michael},
        new PhonebookEntry() { FirstName = "Steven", LastName = "Buchanan", PhoneNumber = "(71) 555-4848", Address = "14 Garrett Hill", Image = Resources.steven },
        new PhonebookEntry() { FirstName = "Margaret", LastName = "Peacock", PhoneNumber = "(206) 555-8122", Address = "4110 Old Redmond Rd.", Image = Resources.margaret1 },
        new PhonebookEntry() { FirstName = "Janet", LastName = "Leverling", PhoneNumber = "(206) 555-3412", Address = "722 Moss Bay Blvd.", Image = Resources.janet1 },
        new PhonebookEntry() { FirstName = "Andrew", LastName = "Fuller", PhoneNumber = "(206) 555-9482", Address = "908 W. Capital Way", Image = Resources.andrew1 },
        new PhonebookEntry() { FirstName = "Nancy", LastName = "Davolio", PhoneNumber = "(206) 555-9857", Address = "507 - 20th Ave. E. Apt. 2A", Image = Resources.nancy1 }
    };
    return entries;
}

7. The next step is to create click event handlers for the buttons:

void radButtonAddToContacts_Click(object sender, EventArgs e)
{
    foreach (ListViewDataItem item in this.radCheckedListBox1.CheckedItems)
    {
        ListViewDataItem contactItem = new ListViewDataItem();
        this.radCheckedListBox2.Items.Add(contactItem);
        //here you can add logic to avoid duplicating contacts
        PhonebookEntry entry = item.Value as PhonebookEntry;
        contactItem["Name"] = entry.FirstName + " " + entry.LastName;
        contactItem["Phone"] = entry.PhoneNumber;
    }
}
void radButtonRemoveFromContacts_Click(object sender, EventArgs e)
{
    while (this.radCheckedListBox2.CheckedItems.Count > 0)
    {
        this.radCheckedListBox2.Items.Remove(this.radCheckedListBox2.CheckedItems[0]);
    }
}

8. The final step is to use the VisualItemFormatting event to style the items in the first RadCheckedListBox. Please note that the checkbox position is changed.

BaseListViewVisualItem item = e.VisualItem;
PhonebookEntry entry = item.Data.Value as PhonebookEntry;
item.Image = entry.Image.GetThumbnailImage(80, 80, null, IntPtr.Zero);
item.Text = "<html>" +
       "<span style=\"font-size:14pt;font-family:Segoe UI;\">" + entry.FirstName + " " + entry.LastName + "</span>" +
       "<br><br><span style=\"font-size:10.5pt;\"><b>Address:</b> <i>" + entry.Address + "</i>" +
       "<br><b>Phone:</b> <i>" + entry.PhoneNumber + "</i></span>";
if (item.Children.Count > 0)
{
    ListViewItemCheckbox checkBoxItem = item.Children[0] as ListViewItemCheckbox;
    checkBoxItem.Margin = new Padding(2);
}

WinForms RadCheckedListBox VisualItemFormatting

Telerik UI for WinForms Learning Resources