How listview selection changed event work. It called twise

≡放荡痞女 提交于 2020-01-21 19:53:52

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25282263/how-listview-selection-changed-event-work-it-called-twise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!