views:

590

answers:

3

Hi all,

In .NET framework, is it possible to set some of the items in the CheckedListBox as "uncheckable" ? I don't want to allow user to check the same items again and add them to a another existing list.

I hope I am clear. Thanks in advance.

+5  A: 

I would set those items as "Indeterminate" in code, and then overwrite the "NewValue" property from the ItemCheck event when the user attempts to check/uncheck them:

public Form1()
{
    InitializeComponent();
    checkedListBox1.Items.Add("Can't check me", CheckState.Indeterminate);
}

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.CurrentValue == CheckState.Indeterminate)
    {
        e.NewValue = CheckState.Indeterminate;
    }
}

The "Can't check me" item in the CheckedListBox can't be modified, because every time the user tries to check/uncheck it, the event handler changes it back. You don't even see the UI update accordingly.

Matt Hamilton
A: 

CheckedListBox... oh man how much do I hate this control. I'd advise you to rethink which control you are using. This thing should almost not be a Control. I wish they'd not included it in the final build of .net. It's just missing some basic things that other controls have.

jcollum
I do agree somewhat. A ListView with checkboxes is a more "standard" control to use for this sort of scenario IMHO.
Matt Hamilton
Thanks to all. I guess I will use ListView with checkboxes. :)
fantoman
A: 
shahkalpesh
Actuall what I am thinking is something like this: We have two lists. User can select and add things from list one to list two. BUT, I don't want to let the user add the same item to the second list for a second time. In other words, list 2 should consist of uniq elements of list1.
fantoman