1

I have list view control where on change of selection, I do check - if selected record count is greater then zero then only enable group box controls else keep it disable. Because, those are controls are related to selected record only. if no record selected then it should not be enable.

Following is my listview's selected changed event:

    Private Sub lv_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lv.SelectedIndexChanged

   If lv.SelectedItems.Count() > 0 Then
     ...
     .
     ResetifNorecordSelectedState(False)
   Else
      ..
    ResetifNorecordSelectedState(True)
   End If

Problem: On each time when user change the record selection then controls goes disabled and follow by enabled state. It makes some inconvenient design to user.

can any one share me solution or what should I change here to correct this issue. ?

Thanks

user3711357
  • 1,425
  • 7
  • 32
  • 54
  • what are trying to do? – Michael B. Aug 13 '14 at 09:30
  • Please find updated question (included some details). – user3711357 Aug 13 '14 at 09:51
  • 1
    it is doing what it is designed to do. Especially with Single Select, when the user changes the selection, you get a notice that A is now deselected, then one that B is now selected. – Ňɏssa Pøngjǣrdenlarp Aug 13 '14 at 11:57
  • 1
    The event is chatty, telling you more than you want to know. You'll need a Timer to avoid the "inconvenient design", start it when you see no selections, stop it when you see one or more. Or just [prevent the user from deselecting an item](http://stackoverflow.com/questions/21605519/prevent-listview-from-deselect). – Hans Passant Aug 13 '14 at 13:00
  • how to do timer. I can't create new listview control as it is implemented with many changes and dependency. Is there any way to override some events or like a way to resolve this. – user3711357 Aug 13 '14 at 13:09

1 Answers1

4

ListView fires a SelectedIndexChanged both when rows are selected and when they are deselected. So clicking a new row fires two events: one for deselecting the old row, another for selecting the new row.

In your SelectedIndexChanged event, schedule another method to be run at idle time, but make sure to one schedule one of them:

// If we haven't already scheduled an event, schedule it to be triggered
// By using idle time, we will wait until all select events for the same
// user action have finished before triggering the event.
if (!_hasIdleHandler) {
    _hasIdleHandler = true;
    Application.Idle += HandleDeferredSelectionChanged;
}

Then in your HandleDeferredSelectionChanged you can do you work:

private virtual void HandleDeferredSelectionChanged(object sender, EventArgs e) {
    // Remove the handler before triggering the event
    Application.Idle -= HandleDeferredSelectionChanged;
    _hasIdleHandler = false;

    // do your checking here
}

These ideas from ObjectListView which already solves many of the problems you are going to have with ListView.

Grammarian
  • 6,774
  • 1
  • 18
  • 32
  • That's a great idea! Someone must have had pretty deep knowledge of the framework to come up with this. Thanks Grammarian for bringing this here :-) – Zegar Mar 26 '15 at 10:34