Click here to Skip to main content
11,704,315 members (51,960 online)
Click here to Skip to main content

Bindable CheckedListBox

, 24 Jul 2006 CPOL 183.2K 5.1K 54
Rate this:
Please Sign up or sign in to vote.
In this article you will see how to bind a CheckedListBox to the data source and use features of multiple selection with data binding.

Sample Image - ExCheckedListBox1.jpg

Introduction

It seems that the .NET CheckedListBox even doesn't have primary binding facilities exists in the ListBox and ComboBox, because it must save multiple values in the database. Current article introducing an extended CheckedListBox that has a single property for setting and getting checked items, and also has binding facilities.

Problems

  • .NET CheckedListBox doesn't have binding facilities.
  • Because CheckedListBox works with an object collection it can't be bound to a data source.
  • CheckedListBox doesn't have a property representing whether item checked and it must be changed or getting data with Methods for one item or a collection of checked items hardly .

Needs

  • Creating a public bindable property that representing checked items and sets checked items based on it.
  • It must be one single property that when sets or gets it saves single primitive type value.
  • Items must be loaded from a data source (it could be lookup table) different from Value data source.
  • Because of list controls nature we must bind it to a column.

Using the code

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

ExCheckedListBox

In the code samples major code belongs to ExCheckedListBox inherited from CheckedListBox of .NET framework.

It's extended CheckedListBox control and has three extra properties:

1. Value property:

This property is type of integer that brings ability to get and set checked item based on one single value. It works with bits in that integer, saves and retrieves values in single column.

get
{
    ///Gets checked items in decimal mode from binary mode
    
    try
    {
        //each item in list has a number that is binary number in decimal mode
        //this number represents that number
        int poweredNumber = 1;
        //loop in all items of list
        for (int i = 0; i < this.Items.Count; i++)
        {
            //if item checked and the value doesn't contains poweredNumber 
            //then add poweredNumber to the value
            if((this.GetItemChecked(i)))
                this.value |= poweredNumber;
            //else if poweredNumber exists in the value remove from it
            else if ((this.value & poweredNumber) != 0)
                this.value -= poweredNumber;

            //raise to the power
            poweredNumber *= 2;
        }
    }
    catch (ArgumentException ex)
    {
        throw ex;
    }
    catch (Exception ex)
    {
        throw ex;
    }


    return this.value;
}
set
{
    ///sets checked items from binary mode converted from decimal value

    this.value = value;
    try
    {
        //each item in list has a number that is binary number in decimal mode
        //this number represents that number
        int poweredNumber = 1;
        //loop in all items of list
        for (int i = 0; i < this.Items.Count; i++)
        {
            //if poweredNumber exists in the value set checked on item
            if ((this.value & poweredNumber) != 0)
                this.SetItemCheckState(i, CheckState.Checked);
            //else remove checked from item
            else
                this.SetItemCheckState(i, CheckState.Unchecked);

            //raise to the power
            poweredNumber *= 2;
        }
    }
    catch (ArgumentException ex)
    {
        throw ex;
    }
    catch (Exception ex)
    {
        throw ex;
    }


}

2. DataSource property:

This property is type of object that brings ability to get and set data source like other collection controls (ListBox and ComboBox). In reality it's base DataSource property used, but it hided in the .NET CheckedListBox because of CheckedListBox logic. So I hide it by new keyword and reused base DataSource protected property.

/// <summary>
/// Gets or sets the data source for this CustomControls.CheckedListBox.
/// Returns:
///    An object that implements the System.Collections.IList or 
//     System.ComponentModel.IListSource
///    interfaces, such as a System.Data.DataSet or an System.Array. The <BR>///    default is null.
///
///Exceptions:
///  System.ArgumentException:
///    The assigned value does not implement the System.Collections.IList or 
//     System.ComponentModel.IListSource
///    interfaces.
/// </summary>
[DefaultValue("")]
[AttributeProvider(typeof(IListSource))]
[RefreshProperties(RefreshProperties.All)]
[Browsable(true)]
public new object DataSource { 
    get 
    {
        return base.DataSource;
    }
    set 
    {
        base.DataSource = value;
        
    }
}

DataSource Property

3. DisplayMember property:

This property is type of string that brings ability to get and set data source specific column like other collection controls (ListBox and ComboBox).

In reality it's base DisplayMember property used, but it hided in the .NET CheckedListBox because of CheckedListBox logic.

So I hide it by new keyword and reused base DisplayMember protected property.

/// <summary>
///   Gets or sets the property to display for this <BR>///   CustomControls.CheckedListBox.
///
/// Returns:
///     A System.String specifying the name of an object property that is <BR>///     contained in the collection specified by the <BR>///     CustomControls.CheckedListBox.DataSource property. The default is <BR>///     an empty string ("").
/// </summary>
[DefaultValue("")]
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, <BR>System.Design, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", 
typeof(UITypeEditor))]
[Browsable(true)]
public new string DisplayMember
{
    get
    {
        return base.DisplayMember;
    }
    set
    {
        base.DisplayMember = value;
        
    }
}

Demo Project

Demo project contains a SQL Script that creates two tables:

Products and Cities that have the same scenario of (presented products in specified cities).

Points of Interest

I wanted to implement another scenario (Questions and Answers) In that scenario we must navigate questions and answer the tests with multiple answers but there is a problem with data binding and this scenario.

License

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

Share

About the Author

Hossein Ghahvei Araghi
Program Manager System Group
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Hossein Ghahvei Araghi
Birth date: 1978
Birth place: Iran
Academic Credentials : BS(Tehran University)
Microsoft Credentials : MCP, MCAD, MCTS 2.0, MCTS 3.5, MCPD 2.0, MCPD 3.5

You may also be interested in...

Comments and Discussions

 
Questionhttp://www.codeproject.com/Articles/14916/Bindable-CheckedListBox#_comments Pin
mansoorttg28-May-12 5:01
membermansoorttg28-May-12 5:01 
GeneralNew Simple Solution Pin
Alejandro Diaz8-May-08 11:20
memberAlejandro Diaz8-May-08 11:20 
QuestionRe: New Simple Solution Pin
José Filipe Néis5-Jun-08 8:13
memberJosé Filipe Néis5-Jun-08 8:13 
GeneralGreat Work Pin
UnRusoDeCaracas1-Apr-08 7:35
memberUnRusoDeCaracas1-Apr-08 7:35 
GeneralAcceptChanges bug Pin
RVW28-Feb-08 2:16
memberRVW28-Feb-08 2:16 
QuestionCheckListBox Pin
Member 234864126-Jan-08 16:21
memberMember 234864126-Jan-08 16:21 
GeneralCheckedListBox does have Datasource, DisplayMember properties. Pin
VB Prog18-Dec-07 9:57
memberVB Prog18-Dec-07 9:57 
GeneralRe: CheckedListBox does have Datasource, DisplayMember properties. Pin
Alejandro Rey7-Apr-08 11:43
memberAlejandro Rey7-Apr-08 11:43 
GeneralRe: CheckedListBox does have Datasource, DisplayMember properties. Pin
Seema Gosain17-Jan-11 21:23
memberSeema Gosain17-Jan-11 21:23 
GeneralVery nice, but... Pin
shaybar25-Oct-07 6:44
membershaybar25-Oct-07 6:44 
QuestionRe: Very nice, but... Pin
RVW28-Feb-08 2:18
memberRVW28-Feb-08 2:18 
AnswerRe: Very nice, but... Pin
shaybar28-Feb-08 22:11
membershaybar28-Feb-08 22:11 
GeneralRe: Very nice, but... Pin
shaybar28-Feb-08 22:16
membershaybar28-Feb-08 22:16 
GeneralBindable CheckedListbox Pin
edgardepuy7-Aug-07 6:09
memberedgardepuy7-Aug-07 6:09 
Generalvery good Pin
abdelrahman khaled farooq22-Apr-07 8:42
memberabdelrahman khaled farooq22-Apr-07 8:42 
QuestionGet stuck in control??? Pin
xtempore11-Mar-07 22:27
memberxtempore11-Mar-07 22:27 
QuestionHow can i get the VALUE and DISPLAY MEMBER for the checked items? Pin
kylai224-Feb-07 22:42
memberkylai224-Feb-07 22:42 
QuestionWhy not use array or list as bound property? Pin
philippe dykmans30-Nov-06 4:26
memberphilippe dykmans30-Nov-06 4:26 
AnswerRe: Why not use array or list as bound property? Pin
Hossein eraghi2-Dec-06 17:42
memberHossein eraghi2-Dec-06 17:42 
GeneralRe: Why not use array or list as bound property? Pin
philippe dykmans16-Dec-06 3:26
memberphilippe dykmans16-Dec-06 3:26 
GeneralVB.Net and additions Pin
Chris Kolkman10-Oct-06 6:10
memberChris Kolkman10-Oct-06 6:10 
GeneralRe: VB.Net and additions Pin
Karl Rhodes6-Mar-07 0:35
memberKarl Rhodes6-Mar-07 0:35 
GeneralRe: VB.Net and additions Pin
GlimmerMan24-Sep-09 3:46
memberGlimmerMan24-Sep-09 3:46 
Questionhow can i get the text of selected item Pin
Amar Chaudhary30-Sep-06 13:29
memberAmar Chaudhary30-Sep-06 13:29 
AnswerRe: how can i get the text of selected item [modified] Pin
kzimny29-Nov-06 8:01
memberkzimny29-Nov-06 8:01 
GeneralRe: how can i get the text of selected item Pin
Amar Chaudhary30-Nov-06 2:55
memberAmar Chaudhary30-Nov-06 2:55 
QuestionBitFieldValue Pin
febus29-Aug-06 1:35
memberfebus29-Aug-06 1:35 
AnswerRe: BitFieldValue Pin
RVW28-Feb-08 2:26
memberRVW28-Feb-08 2:26 
Generalsalam Pin
mohammadjoon2-Aug-06 3:59
membermohammadjoon2-Aug-06 3:59 
GeneralVery cool Pin
mikeperetz2-Aug-06 3:24
membermikeperetz2-Aug-06 3:24 
Generalgood job Pin
Gulfraz Khan25-Jul-06 7:23
memberGulfraz Khan25-Jul-06 7:23 
GeneralRe: good job Pin
Gulfraz Khan27-Jul-06 4:37
memberGulfraz Khan27-Jul-06 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 24 Jul 2006
Article Copyright 2006 by Hossein Ghahvei Araghi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid