Click here to Skip to main content
15,879,096 members
Articles / Programming Languages / C#
Tip/Trick

Avoiding Whitespace Clicking in CheckedListBox

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 Sep 2013CPOL2 min read 21.2K   5   4
Prevent a checkedListBox from toggling check state when clicking whitespace

Introduction

This piece of code is intended to prevent an issue of having a checkedListBox with whitespace in it, and when you click the whitespace, it will toggle the state of a selected item (if one is selected).

Background

I came up with this code after I had an issue with a program of mine where a checkedListBox could have whitespace in it. I had the settings for the item set for checkOnClick, so when the user would click in the whitespace of the checkedListBox and had an item selected, it would change the checked state of the item selected.

Using the Code

This piece of code is pretty simple, actually simpler than the one I originally had. Recently, I found myself using this code again, but after reading a comment from Alan N, I found myself looking for a better way to do it.

The original code involved using a MouseUp event, however now it uses the ItemCheck event. The ItemCheck event is called before the actual item's check state changes. The following event looks like the following:

C#
private void checkedListBox1_ItemCheck 
(object sender, ItemCheckEventArgs e) //checks the item being checked before it does
{
    //if the click isn't on an item (and on the white space)
    if (checkedListBox1.IndexFromPoint(checkedListBox1.PointToClient(Cursor.Position).X, 
    checkedListBox1.PointToClient(Cursor.Position).Y) <= -1) 
    {
        e.NewValue = e.CurrentValue;
    }
}   

Now the thing is however, the ItemCheck event does not offer a parameter for getting the cursors location with relation to the checkedListBox like you might in the MouseUp event. So to get around this issue, PointToClient is used. PointToClinet from how I understand it will give the cursors location in relation to the item it's linked to, which in this case is checkedListBox1, and not in relation to the cursor's position on the whole screen.

This simple little code works great for the problem listed above, and unlike before where it would actually change the toggle state then revert it back if invalid, this prevents the check all together if it's invalid.

Points of Interest

I spent a while looking for some code to do something like this, but wasn't having a lot of luck. Then I decided to look into it and write something of my own.

History

The code to do this was updated. The newest version is smaller, and more effective. Special thanks to Alan N, whose comment helped me find a better way (the IndexFromPoint especially).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
United States United States
I am current a college student working on my Computer Science Security degree.

I quickly fell in love with programming after I started my first CS class (Java ... the first programming language I really learned). Since then I write programs for fun and find much enjoyment in them.

Comments and Discussions

 
QuestionCaveat Pin
Byngl26-Feb-21 9:00
Byngl26-Feb-21 9:00 
Questionperfect Pin
Ivan Ferrer18-Feb-21 21:35
Ivan Ferrer18-Feb-21 21:35 
QuestionA few thoughts Pin
Alan N18-Jul-12 9:01
Alan N18-Jul-12 9:01 
GeneralRe: A few thoughts Pin
James IV24-Jul-12 23:37
James IV24-Jul-12 23:37 
Hmm it's interesting you mentioned the MouseClick event occurs before the ItemCheck. I'll have to look into that a little more.

However, I usually like to use MouseUp because if the user is dragging around the cursor for some reason it will always check where they left off (usually giving the intended value needed). Does MouseClick offer similar results? I mean I assume it's a combination of MouseUp and MouseDown ... I guess I'll have to do some testing there to

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.