Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Visual Basic
Article

Workaround for disappearing checks in the CheckedListBox control

Rate me:
Please Sign up or sign in to vote.
4.53/5 (13 votes)
24 Apr 20032 min read 163.6K   1.2K   25   21
The CheckedListBox control has a serious problem! It forgets the checks when its DataSource property is set. This is the most effective workaround I have found.

Image 1

Introduction

As many reported on the newsgroups (including me!), the CheckedListBox control forgets which item is checked when the items have been filled using the DataSource property and when its Visible property is set to false, then back to true.  The problem also occurs when it is hosted on a TabControl and tabs are flipped.

I first ran into this issue in my first week of .NET development.  Very frustrating start!  Since then, I tried many workarounds but all were just an extreme pain in the neck, especially when I was trying to use them on more complex forms. 

Those workarounds included never setting the Visible property of the CheckedListBox to False.  Rather, I moved the control outside the form (ex: -5000, -5000).  It worked (most of the time), but some clients reported the problem even with this. 

I also tried saving all the checks in an array before setting Visible to False, then putting them back in the CheckedListBox on Visible=true but once again, I felt it was a quite ineffective way.

Back then, I did not know that this bug was related to the Datasource property.  I recently figured this out after pulling my hair for a couple hours.  I finally came up with a great and easy workaround...

The Workaround

As I mentioned earlier, the problem only occurs when DataSource is used to add the items to the CheckedListBox.  So the idea is, you guessed it, to NOT use DataSource! :)  Here's the code I came up with:

VB
Private Sub SetClBoxDataSource(ByVal clb As CheckedListBox, 
                 ByVal dt As DataTable)

    'Clear it
    clb.Items.Clear()

    'Fill it
    Dim x As Integer
    For x = 0 To dt.DefaultView.Count - 1
        clb.Items.Add(dt.DefaultView.Item(x))
    Next

End Sub

Basically, what this code does is adding the items one by one using a simple loop.  Now, instead of using this code to fill up the CheckedListBox:

VB
CheckedListBox.DataSource = DataTable

You should do:

VB
SetClBoxDataSource(CheckedListBox, DataTable)

It couldn't be easier!  Only one line of code to change!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions

 
GeneralProblem in displaying the Checked items in WPF [modified] Pin
SabariGE8-Feb-09 22:03
SabariGE8-Feb-09 22:03 
GeneralCheckboxlist Dispaly Pin
n_gchaitra16-Aug-07 22:56
n_gchaitra16-Aug-07 22:56 
GeneralDropdown binding Pin
Anonymous21-Aug-05 1:52
Anonymous21-Aug-05 1:52 
GeneralRe: Dropdown binding Pin
morteza5711-Jul-06 22:28
morteza5711-Jul-06 22:28 
GeneralThis problem Solved Pin
acukier5-Apr-05 10:58
acukier5-Apr-05 10:58 
GeneralRe: This problem Solved Pin
Carl Mercier5-Apr-05 11:16
Carl Mercier5-Apr-05 11:16 
GeneralRe: This problem Solved Pin
Anonymous5-Jul-05 11:05
Anonymous5-Jul-05 11:05 
GeneralCheckedListBox Problem Pin
vrushaliD5-Jan-05 18:42
vrushaliD5-Jan-05 18:42 
GeneralUsing custom list class Pin
TekProfessional31-Aug-04 12:59
TekProfessional31-Aug-04 12:59 
GeneralI "receive"..System.Data.DataRowView Pin
sjkdo21-Apr-04 10:25
sjkdo21-Apr-04 10:25 
GeneralRe: I "receive"..System.Data.DataRowView Pin
Anonymous21-Apr-04 10:30
Anonymous21-Apr-04 10:30 
GeneralRe: I "receive"..System.Data.DataRowView Pin
sjkdo24-Apr-04 2:04
sjkdo24-Apr-04 2:04 
GeneralTry this Pin
Ketan Shukla25-Mar-04 10:42
Ketan Shukla25-Mar-04 10:42 
GeneralRe: Try this Pin
Ketan Shukla25-Mar-04 10:53
Ketan Shukla25-Mar-04 10:53 
GeneralRe: Try this Pin
vrushaliD5-Jan-05 18:45
vrushaliD5-Jan-05 18:45 
GeneralNo Datasource for CheckedListBox Pin
DalePres10-Feb-04 13:48
DalePres10-Feb-04 13:48 
GeneralRe: No Datasource for CheckedListBox Pin
ChrisWard27-Oct-04 4:29
ChrisWard27-Oct-04 4:29 
Generaleasiest way Pin
billzelf12-Sep-03 6:42
billzelf12-Sep-03 6:42 
GeneralRe: easiest way Pin
Ricalawaba21-Mar-04 15:14
Ricalawaba21-Mar-04 15:14 
GeneralRelated link Pin
Meysam Mahfouzi25-Apr-03 17:24
Meysam Mahfouzi25-Apr-03 17:24 
GeneralRe: Related link Pin
Carl Mercier26-Apr-03 4:18
Carl Mercier26-Apr-03 4:18 
Meisi,

I just read the thread. People are reporting the problem with the ListBox (which CheckedListBox inherits from), but there doesn't seem to be a real solution there.

Anyway, I think this workaround was worth publishing since I never could find an acceptable solution to the problem on Usenet or any website.

Have a nice day!

Carl

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

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