By default, double-clicking a ListViewItem toggles its Checked state. I only want the Checked state to be changed by clicking an the item's checkbox or pressing the space bar while an item is highlighted. Is this easy to do?
+1
A:
The solution involves 3 events and one state variable of type bool:
private bool inhibitAutoCheck;
private void listView1_MouseDown(object sender, MouseEventArgs e) {
inhibitAutoCheck = true;
}
private void listView1_MouseUp(object sender, MouseEventArgs e) {
inhibitAutoCheck = false;
}
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (inhibitAutoCheck)
e.NewValue = e.CurrentValue;
}
The item check enables to avoid the transition to another check state (called before the ItemChecked event). The solution is simple and sure.
To find it out I made a small test with different events:
When clicking:
- MouseDown
- Click
- MouseClick
- MouseUp
- ItemCheck
- ItemChecked
When double clicking:
- MouseDown
- ItemSelectionChanged
- SelectedIndexChanged
- Click
- MouseClick
- MouseUp
- MouseDown
- ItemCheck
- ItemActivate
- DoubleClick
- MouseDoubleClick
- MouseUp
jdehaan
2009-09-10 19:27:48