How listview selection changed event work. It called twise

橙三吉。 提交于 2019-12-02 06:24:04

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.

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