.NET的ListView:事件变更后选择事件、NET、ListView

2023-09-04 02:46:47 作者:软肋

在ListView控件,我需要一个事件之后选择更改为火,但只有一次,每个用户操作。如果我只是使用的SelectedIndexChanged,该事件触发两次在大多数情况下(曾经当previous元素是选择,而在选择新的元素再一次)的事件,如果用户只点击了控制,一旦

On the ListView control, I need an event to fire after the selection changes, but only once per user action. If I simply use the SelectedIndexChanged, that event fires twice in most cases (once when the previous element is unselected and once more when the new element is selected) event if the user only clicked on the control once.

这两个事件火势迅速一前一后,但计算我选择何时更改,需要一定的时间(几乎是第二个),所以它是做两次,减慢了界面。

The two events fire quickly one after the other, but the computation I do when the selection changes takes time (almost a second) so it is done twice and slows down the interface.

我想要做的就是每个用户操作仅做一次(被选中做了新的元素之前是没有用的),但我不知道如果该事件仅仅是第一一对(以跳过所述第一计算),因为如果用户只是取消所选元素,将火只有一次。

What I want to do is only do it once per user action (doing it before the new element is selected is useless) but I have no way of knowing if the event is just the first of a pair (in order to skip the first computation) because if the user just deselected the selected element, it will fire only once.

我不能使用单击或鼠标点击事件,因为这些不火,在所有如果用户点击列表之外,以取消选择。

I can't use the Click or MouseClick events because those don't fire at all if the user clicked outside the list to remove the selection.

感谢您的帮助。

推荐答案

下面是我在VB.NET解决方案,使用中的 ObjectListView 所建议的Grammarian:

Here's my solution in VB.NET, using the technique described in ObjectListView as suggested by Grammarian:

Private idleHandlerSet As Boolean = False

Private Sub listview1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listview1.SelectedIndexChanged
    '' may fire more than once
    If Not idleHandlerSet Then
        idleHandlerSet = True
        AddHandler Application.Idle, New EventHandler(AddressOf listview1_SelectionChanged)
    End If
End Sub

Private Sub listview1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '' will only fire once
    idleHandlerSet = False
    RemoveHandler Application.Idle, New EventHandler(AddressOf listview1_SelectionChanged)
    DoSearch()
End Sub