Click here to Skip to main content
15,891,719 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
Is it possible that when you select multiple items in the CheckedListBox rest was disabled?
Posted
Comments
Maciej Los 11-May-12 10:17am    
Not clear... Please provide more details.
Sergey Alexandrovich Kryukov 11-May-12 11:34am    
I think it's clear enough. OP wants to access enabled status of each item. Please see my answer.
--SA
Maciej Los 11-May-12 11:40am    
Maybe, but not enough for me...

To best of my understanding, this control provides only the the access to the checked status of each item, which is 3-state, but not to enable/disable individual items.

Need a work-around? It's not too hard to create a custom control with somewhat similar behavior but with additional feature: access to each individual check box. All you need is: 1) create a custom control derived from System.Windows.Forms.Panel, 2) populate it with the instances of System.Windows.Forms.CheckBox, 3) use the property ScrollableControl.AutoScroll with proper auto-scroll margins to allow scrolling, 4) advanced feature: for each instance of the check box, add a handler of the event KeyDown to simulate shifting focus to next/previous child check box, override Panel.OnPaint to show some visual feed back of the selected item by painting corresponding part of the background in different "selection" color.

It would be quite a piece of work, good custom control programming exercise, but it will take time, so think by yourself if you really want it.

—SA
 
Share this answer
 
v3
Comments
VJ Reddy 11-May-12 11:36am    
Good answer and detailed suggestion. 5!
Sergey Alexandrovich Kryukov 11-May-12 11:39am    
Thank you, VJ.
--SA
Maciej Los 11-May-12 11:41am    
I agree with you. +5
Sergey Alexandrovich Kryukov 11-May-12 11:44am    
Thank you.
--SA
gruby555 11-May-12 11:57am    
Thanks for your answer
This solution is a little labor intensive, so use the option checkedListBox1.CheckedItems.Count and beyond a certain value of more items will not be selected.
For the original question the Solution 2 given by SAKryukov is good.

For the requirement

use the option checkedListBox1.CheckedItems.Count and beyond a certain value of more items will not be selected.

stated by OP in the comment to Solution 2, one of the following Options can be used.

1. By deriving from the CheckedListBox class

Subclass the CheckedListBox as LimitedSelectCheckedListBox. In the subclass add a Property MaxSelectCount to hold the maximum number of selections allowed.
In the constructor set the default value of this property. Say 1.
Override the OnItemCheck method and set the NewValue to Unchecked if the CheckedItems.Count == MaxSelectCount.
Set the MaxSelectCount property of the LimitedSelectCheckedListBox either in the designer or in the Code.

C#
public class LimitedSelectCheckedListBox : CheckedListBox{
    public int MaxSelectCount { get; set; }
    public LimitedSelectCheckedListBox() {
        CheckOnClick = true;
        MaxSelectCount = 1;
    }
    protected override void OnItemCheck(ItemCheckEventArgs ice) {
        if (CheckedItems.Count == MaxSelectCount)
            ice.NewValue = CheckState.Unchecked;
        else
            base.OnItemCheck(ice);
    }
}


2. Subscribe to the CheckedListBox.ItemCheck and disable selection
C#
//Declare a field to hold the maximum number of selections allowed.
int maxSelectCount
//Subscribe to the <code>ItemCheck </code>event say in the Form.Load event
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
//Set the maximum select count.
maxSelectCount = 3;


private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
//Check if the CheckedItems Count equals the maximum number of 
//selections, if so disable selection
    if ((sender as CheckedListBox).CheckedItems.Count == maxSelectCount) 
        e.NewValue = CheckState.Unchecked;
}
 
Share this answer
 
Comments
Sandeep Mewara 12-May-12 3:59am    
Nice! 5+
VJ Reddy 12-May-12 4:07am    
Thank you, Sandeep.
Sergey Alexandrovich Kryukov 14-May-12 15:58pm    
Good, a 5.
--SA
VJ Reddy 14-May-12 16:43pm    
Thank you, SA.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900