Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-03
2
Medium Priority
?
1,966 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 41

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 1500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

926 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