A Custom CheckedListBox with Datasource Implementation (Bindable)

Jan 17, 2008

1 min read

C#3.0

.NET3.5

C#

.NET

Dev

Intermediate

Author picture

by Ricardo Cuello

Contributor

99k Views
CustomCheckedListBox

Introduction

Due to the fact that .NET CheckedListBox does not have out of the box binding facilities, this article implements an extension of the CheckedListBox which can be bindable. Moreover, it implements the DisplayMember and ValueMember properties in order to get an array of IDs containing the respective checked items.

Approach

We have to implement four extra properties:

  • DataSource
  • DisplayMember
  • ValueMember
  • ValueList

You already know the use of the first three but the last one is a List<int>. I chose int because we need the ID and most of the times in the lookup table, this ID is a number. I chose a List because the user can check more than one item, hence we need a list.

Using the Code

To use this code, you have to create an object whose type is cCheckedListBox and add it to your form:

CS
cCheckedListBox cbGenreList = 

To retrieve the values of the checked items :

CS
List <int> selectedValues;
selectedValues= cbGenreList.ValueList;

To check some items by ID:

CS
List<int> myValues = 

History

  • 17th January, 2008: Article posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)