Solved

How can I select all the items in a System.Windows.Forms.CheckedListBox

Posted on 2014-04-03
2
1,734 Views
Last Modified: 2014-04-15
I need to select all the items in CheckedListBox on the check / uncheck of a checkbox click event.
The CheckedListBox is bound to a Generic List as its datasource

 chkListBox.CheckOnClick = true;
                chkListBox.DataSource = listData;
                chkListBox.DisplayMember = "Description";
                chkListBox.ValueMember = "Value";

I have also a SelectAll checkbox, when the users checks the SelectAll I want to have all the items in the chkListBox checked.
When the user unchecks the SelectAll checkbox I want all the items unchecked.
I am using .NEt 4.0
0
Comment
Question by:countrymeister
2 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39976814
credit: http://www.dotnetcurry.com/showarticle.aspx?ID=77

<script language="javascript" type="text/javascript">
 
function CheckBoxListSelect(cbControl, state)
{    
       var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount= chkBoxList.getElementsByTagName("input");
        for(var i=0;i<chkBoxCount.length;i++) 
        {
            chkBoxCount[i].checked = state;
        }
        
        return false; 
}
 
</script>


<asp:CheckBoxList ID="cbl1" runat="server"></asp:CheckBoxList><br />
 
Select <a id="A1" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl1.ClientID %>',true)">All</a>
| <a id="A2" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl1.ClientID %>',false)">None</a>    
 
    <br />
    <br />
 
    <asp:CheckBoxList ID="cbl2" runat="server">
    </asp:CheckBoxList>
    Select <a id="A3" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl2.ClientID %>',true)">All</a>
 
| <a id="A4" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl2.ClientID %>',false)">None</a>

Open in new window


It's a lot easier to use the links.  If you do the checkbox inside the listbox than technically you should deselect the "checkall" box if they uncheck any other box.

You would also need to add logic to check the checkall box if they checked each of the items.

EG:
 If checkbox for check all is checked
   check all items
  if checkbox for check all is unchecked
    uncheck all items
  if another checkbox is unchecked
   uncheck checkall (don't uncheck the rest)
  if all but select all is checked
   check check all.

VS having the links.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39976880
Unfortunately there isn't a simple SelectAll() method, so you have to resort to a simple loop:
bool checked = YourSelectAllCheckBox.Checked;
for (int i = 0; i < checkedListBox1.Items.Count; ++i)
    checkedListBox1.SetSelected(i, checked);

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now