The WinForms CheckedListBox
control has 2 default behaviors when clicking with a mouse:
- In order to check/uncheck an item you're required to click an item twice. The first click selects the item, and the second toggles the check state.
- In addition, one subsequent click of the same item will toggle that item's checked state.
As a convenience feature I needed to allow users to toggle the selection in one click. I have achieved this, so now default behavior #1 above is achieved in one click. The problem is behavior #2 no longer works correctly when clicking the same (i.e., currently selected) item. It works fine when jumping between items, which is desired, but it requires up to 4 clicks on the same item.
My workaround for this is to call the toggling logic twice if the user selects the same item repeatedly. So on to my questions:
- This works, but why? What's the real underlying issue?
- Is there a better way to achieve this so I can get it working like default behavior #2 without calling the method twice and keeping track of my last selection?
Oddly enough debugging the code reveals that the checked state has changed but it doesn't appear on the UI side till it's called twice. I thought it might be threading related but it's not a re-entrant event being triggered that might need BeginInvoke
usage.
Here's my code:
using System.Linq;
using System.Windows.Forms;
namespace ToggleCheckedListBoxSelection
{
public partial class Form1 : Form
{
// default value of -1 since first item index is always 0
private int lastIndex = -1;
public Form1()
{
InitializeComponent();
CheckedListBox clb = new CheckedListBox();
clb.Items.AddRange(Enumerable.Range(1, 10).Cast<object>().ToArray());
clb.MouseClick += clb_MouseClick;
this.Controls.Add(clb);
}
private void clb_MouseClick(object sender, MouseEventArgs e)
{
var clb = (CheckedListBox)sender;
Toggle(clb);
// call toggle method again if user is trying to toggle the same item they were last on
// this solves the issue where calling it once leaves it unchecked
// comment these 2 lines out to reproduce issue (use a single click, not a double click)
if (lastIndex == clb.SelectedIndex)
Toggle(clb);
lastIndex = clb.SelectedIndex;
}
private void Toggle(CheckedListBox clb)
{
clb.SetItemChecked(clb.SelectedIndex, !clb.GetItemChecked(clb.SelectedIndex));
}
}
}
To reproduce my problem comment out the lines mentioned in the code comments and follow these steps:
- Click the item at index 2 - state changes to checked.
- With the current item selected, click it again - state does not change. Expected: unchecked. Click it a few times and it finally switches.
Thanks for reading!