CheckedListBox Control - Only checking the checkbox when the actual checkbox is clicked

Oxymoron picture Oxymoron · Jan 19, 2010 · Viewed 10.3k times · Source

I'm using a CheckedListBox control in a small application I'm working on. It's a nice control, but one thing bothers me; I can't set a property so that it only checks the item when I actually check the checkbox. What's the best way to overcome this? I've been thinking about getting the position of the mouseclick, relative from the left side of the checkbox. Which works partly, but if I would click on an empty space, close enough to the left the current selected item would still be checked. Any ideas regarding this?

Answer

Wilfred picture Wilfred · Jan 2, 2011

I know this thread's a bit old, but I don't think it's a problem to offer another solution:

private void checkedListBox1_MouseClick(object sender, MouseEventArgs e)
{
    if ((e.Button == MouseButtons.Left) & (e.X > 13))
    {
        this.checkedListBox1.SetItemChecked(this.checkedListBox1.SelectedIndex, !this.checkedListBox1.GetItemChecked(this.checkedListBox1.SelectedIndex));
    }
}

(With the value of CheckOnClick = True).

You could use that thingy with the rectangle, but why make it more complex the it needs to.