列表视图选择更改事件如何工作.它叫twise [英] How listview selection changed event work. It called twise

查看:103
本文介绍了列表视图选择更改事件如何工作.它叫twise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列表视图控件,在选择更改时,我会进行检查-如果所选记录数大于零,则仅启用组框控件,否则将其禁用.因为,这些控件仅与所选记录相关.如果未选择任何记录,则不应启用该记录.

以下是我的列表视图的选定更改事件:

    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

问题:每次用户更改记录选择时,控件将被禁用,然后进入启用状态.这给用户带来了一些不便的设计.

任何人都可以分享我的解决方案,或者我应该在此处更改以纠正此问题. ?

谢谢

解决方案

在选择行和取消选择行时,ListView都会触发SelectedIndexChanged.因此,点击新行会引发两个事件:一个事件是取消选择旧行,另一个事件是选择新行.

在您的SelectedIndexChanged事件中,安排另一种在空闲时间运行的方法,但请确保安排其中一种:

// 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;
}

然后在您的HandleDeferredSelectionChanged中,您可以工作:

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

    // do your checking here
}

来自 ObjectListView 的这些想法已经解决了ListView可能遇到的许多问题. /p>

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

解决方案

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.

这篇关于列表视图选择更改事件如何工作.它叫twise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆