Solved

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

Posted on 2014-04-03
2
1,845 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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