I'm trying to detect when the user selects a new item in a ListView and when he deselects everything (by clicking on an empty area on the ListView), but I'm having a hard time getting it right. I need this to enable or disable a couple of "Move item up" and "Move item down" buttons. I think the best way to do this is to handle the ItemSelectionChanged
event, so I have this, which seems pretty obvious:
private void lstItems_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
if (e.IsSelected) {
cmbMoveUp.Enabled = true;
cmbMoveDn.Enabled = true;
} else {
cmbMoveUp.Enabled = false;
cmbMoveDn.Enabled = false;
}
}
It works fine when the user deselects everything, but the problem is that this event is fired twice when the user selects another item: one time for deselecting the current item and another for selecting the new one. This causes some blinking on the "Move item up" and "Move item down" buttons, because it will first disable the buttons (because the current item was deselected) and then enable them again (when the new item is selected).
Anyone knows how can I solve this issue? I've ran out of ideas.
Thanks in advance.
I never used listView, but I think you can remove else part of lstItems_ItemSelectionChanged method, then in if part you can first disable all buttons and then enale just two related buttons. lstItems_ItemSelectionChanged event fires two time, first time "if" does'nt fire and second time "if" will fire. so your problem will be solved.