Skip to main content
Email Password   helpLost your password?
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:

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:

cCheckedListBox cbGenreList = new cCheckedListBox();
cbGenreList.Location = new Point(8, 20);
cbGenreList.Size = new Size(130, 180);
this.grpGenres.Controls.Add(cbGenreList);

// Let's bind it to data from a Database
var GenreList = from c in databasebObjectContext.Genres orderby c.gnDescription select c;
cbGenreList.DataSource = GenreList.ToArray();
cbGenreList.DisplayMember = "gnDescription";
cbGenreList.ValueMember = "gnNumber";

To retrieve the values of the checked items :

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

To check some items by ID:

List<int> myValues = new List<int>(); 
myValues.Add(44);
myValues.Add(45);
myValues.Add(46);
cbGenreList.ValueList = myValues;

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSmall Mod Pin
Roger Willcocks
17:51 22 Apr '09  
GeneralGenerics Are Wonderful :) Pin
Roger Willcocks
17:12 22 Apr '09  
GeneralInitialization error Pin
Penko Mitev
7:16 18 Mar '09  
AnswerRe: Initialization error Pin
Radu Martin
5:00 31 Mar '09  
GeneralRe: Initialization error Pin
Pinx
3:21 1 Oct '09  
QuestionBinding the list Pin
Monir Sabbagh
3:50 5 Feb '09  
AnswerRe: Binding the list Pin
Monir Sabbagh
6:31 7 Feb '09  
GeneralThanks Pin
Thomas Wells
10:26 17 Jan '09  
GeneralList Pin
José Filipe Néis
4:11 5 Jun '08  


Last Updated 16 Jan 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009