I have a ListView with the property CheckBoxes set to true and with the following event handler:
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
listView1.Items[e.Index].Selected = e.NewValue == CheckState.Checked ? true : false;
}
Using the keyboard:
I can move the selection left or right using the arrow keys, and (un)check an item by using the Space key. I can select multiple items using Shift + arrow keys.
The problem: when pressing the Space key to uncheck one of the checked items, the selection is automatically set to be a single item, the currently focused item.
Using the mouse:
Selection and checked elements seem to be synchronized, until I check two items and then left-click on one of their two check boxes, moment when the selection is cleared and checked items all become unchecked.
Example screenshots:
Initial state:
then after left-clicking on the first check box and then left clicking on the second check box:
then after left-clicking on any of the two check boxes, the initial state again:
Expected behavior: one of the two check boxes (the one that was not clicked in the last step) should stay checked and selected.
Note: I wish to be able to use other Views of the ListView as well, LabelEdit property, drag & drop, icons, multiselect, groups.
I thought of the possibility of knowing the coordinates of the checkbox rectangle, and using the MouseDown event, or even drawing my own checkboxes but maybe there is an easier or better way.
Update 1: In File Explorer in Windows 10 File Explorer I can enable a function that makes this possible:
Here the first folder (hidden folder) is unchecked and not selected, and the second and third items are selected either through the checkbox or through Ctrl+click, or through both.
Another screenshot:
I find the separation of the check boxes and selection useless, I think it is more intuitive for the user if they are combined.
I am trying to use selection and also check boxes for selecting which groups/folders to show in my application.
Update 2: About the code posted in this related question:
There are some bugs inside that code, one is the following:
Steps to reproduce:
- Modify the designer code to add 5 items in total to the ListView.
- At start of program the first item is focused (neither selected nor checked).
- Pressing the right arrow key moves the focus to the second item.
- When I keep Shift pressed and press the right Arrow key, the behavior is wrong: the second and third items are selected and checked, and after pressing again the right arrow while Shift is pressed, only
item3anditem4get selected - more exactlyitem2anditem3are checked, anditem3anditem4are selected. Releasing the Shift key and clicking in empty space makes only theitem3checked, and none are selected.
The answer of Aleksa Ristic has some bugs:
- double click on label checks/unchecks and the selection state is the opposite;
- rubberband selection does not check the checkboxes;
- once an item is selected by clicking on label, checking it deselects.
Update 3:
The answer of Aleksa Ristic still has some bugs, I see these now:
I start the program then I either:
- directly click on either the label or the checkbox;
- move the focus using the arrow keys;
- press Space or try a Shift selection;
and I always get System.StackOverflowException on the same line i.Selected = false; (near line 85).
When the user clicks out of everything and deselects, I would like to make the currently selected and checked items bold and when the user clicks out of everything, the selection and checked items are cleared.
When the user clicks on a label I would like the same behavior like when it clicks on the checkbox near that label.
The rubberband works really well now.




ListView- you can select multiple items and collectively check/uncheck them with a single click on the checkbox. What exactly are you trying to achieve? - C.Evenhuis