Set Indeterminate state with mouse in CheckedListBox
-
14-03-2021 - |
Question
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;
}
}
OTHER TIPS
I translated the offered suggestion from C# to VB as follows
Private Sub CheckedListBoxCriteria_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBoxCriteria.ItemCheck
Select Case e.CurrentValue
Case CheckState.Checked
e.NewValue = CheckState.Unchecked
Exit Select
Case CheckState.Indeterminate
e.NewValue = CheckState.Checked
Exit Select
Case CheckState.Unchecked
e.NewValue = CheckState.Indeterminate
Exit Select
End Select
End Sub
Worked like a charm. I would have sworn I tried something similar to that, but I didn't get it right. But this worked. Thank you so very much. So simple. Someday I'll figure this out. Teaching myself consists of coming up with an idea and digging around until I find a clue.