Click here to Skip to main content
Licence 
First Posted 24 Apr 2003
Views 130,328
Bookmarked 24 times

Workaround for disappearing checks in the CheckedListBox control

By | 24 Apr 2003 | Article
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.

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:

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:

    CheckedListBox.DataSource = DataTable

You should do:

    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

About the Author

Carl Mercier

Web Developer

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralProblem in displaying the Checked items in WPF [modified] PinmemberSabariGE22:03 8 Feb '09  
GeneralCheckboxlist Dispaly Pinmembern_gchaitra22:56 16 Aug '07  
GeneralDropdown binding PinsussAnonymous1:52 21 Aug '05  
GeneralRe: Dropdown binding Pinmembermorteza5722:28 11 Jul '06  
GeneralThis problem Solved Pinmemberacukier10:58 5 Apr '05  
GeneralRe: This problem Solved PinmemberC a r l11:16 5 Apr '05  
GeneralRe: This problem Solved PinsussAnonymous11:05 5 Jul '05  
GeneralCheckedListBox Problem PinmembervrushaliD18:42 5 Jan '05  
GeneralUsing custom list class Pinmemberdcrane12:59 31 Aug '04  
GeneralI "receive"..System.Data.DataRowView Pinmembersjkdo10:25 21 Apr '04  
GeneralRe: I "receive"..System.Data.DataRowView PinsussAnonymous10:30 21 Apr '04  
GeneralRe: I "receive"..System.Data.DataRowView Pinmembersjkdo2:04 24 Apr '04  
GeneralTry this PinsussKetan Shukla10:42 25 Mar '04  
GeneralRe: Try this PinmemberKetan Shukla10:53 25 Mar '04  
GeneralRe: Try this PinmembervrushaliD18:45 5 Jan '05  
GeneralNo Datasource for CheckedListBox PinmemberDalePres13:48 10 Feb '04  
GeneralRe: No Datasource for CheckedListBox PinmemberChrisWard4:29 27 Oct '04  
Generaleasiest way Pinmemberbillzelf6:42 12 Sep '03  
GeneralRe: easiest way PinmemberRicalawaba15:14 21 Mar '04  
GeneralRelated link PinmemberMeisi17:24 25 Apr '03  
GeneralRe: Related link PinmemberCarl Mercier4:18 26 Apr '03  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 25 Apr 2003
Article Copyright 2003 by Carl Mercier
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid