Search code examples
vb.netcontrolscheckedlistboxtri-state-logic

Set Indeterminate state with mouse in CheckedListBox


I need some assistance and I'm beating my head against the wall. I have an application that uses a tri-state CheckedListBox. I use the three states for specific purposes:

Checked means tech performed an action Unchecked means the tech didn't perform the action Indeterminate means that the tech didn't perform the action because it was unnecessary.

I need to be able toggle, with the mouse from Checked to Unchecked to Indeterminate to Checked as necessary. If I were using a CheckBox and ThreeState were set to True, this is exactly what would happen, but it appears that the only way to set the Indeterminate state in the CheckedListBox is through code.

Could someone give me an idea of what to do? It boggles my mind that it's not a Property you can set like you can in CheckBox.

I think what throws me is that no one seems to have needed this functionality before. I have found nothing on Google about how one might do this, or asking the question.


Solution

  • I don't think that there is a property in the control to control this behavior, but it is easy to implement in code:

        void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            switch (e.CurrentValue)
            {
                case CheckState.Checked:
                    e.NewValue = CheckState.Unchecked;
                    break;
    
                case CheckState.Indeterminate:
                    e.NewValue = CheckState.Checked;
                    break;
    
                case CheckState.Unchecked:
                    e.NewValue = CheckState.Indeterminate;
                    break;
            }
        }