Solved

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

Posted on 2014-04-03
2
1,701 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 39

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

14 Experts available now in Live!

Get 1:1 Help Now