列表视图ItemSelectionChanged闪光两次? [英] Listview ItemSelectionChanged fires twice?

查看:405
本文介绍了列表视图ItemSelectionChanged闪光两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序在C#中使用ListView控件。该ListView控件显示待办事项列表,我使用了ItemSelectionChanged事件来处理更新。

的问题是,两次,每次我试图使更新时间'ItemSelectionChanged事件触发。

在ItemSelectionChanged事件refreshs的形式重新present更新(即从列表中删除项)。

有没有办法从刷新后烧制禁用事件?

UPDATE1:

 私人无效listView1_ItemSelectionChanged(对象发件人,ListViewItemSelectionChangedEventArgs E)
    {
        如果(e.IsSelected)
        {
            listView1.Items [e.ItemIndex]上卸下摆臂();

            listView1.SelectedIndices.Clear();
            listView1.Focus();

            listView1.Update();
        }
        其他
        {

        }

    }
 

解决方案

是的,它会触发两次。一旦因为previously所选项目成为选,再次为新选择的项目。你只需要确保你看到的评选活动:

 私人无效listView1_ItemSelectionChanged(对象发件人,ListViewItemSelectionChangedEventArgs E){
        如果(e.IsSelected){
            //更新表
            // ...
        }
    }
 

I have a Winforms App in C# with a ListView control. This ListView shows a list of TO-DO items and I am using the 'ItemSelectionChanged' event to handle updates.

The problem is that the 'ItemSelectionChanged' event fires twice each time I try to make an update.

The ItemSelectionChanged event refreshs the form to represent the updates (ie remove item from list).

Is there a way to disable the event from firing after the refresh?

UPDATE1:

private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {   
        if (e.IsSelected)
        {                
            listView1.Items[e.ItemIndex].Remove();

            listView1.SelectedIndices.Clear();
            listView1.Focus();

            listView1.Update();
        }
        else
        {

        }

    }

解决方案

Yes, it will fire twice. Once because the previously selected item became unselected, again for the newly selected item. You just have to make sure you see the selection event:

    private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
        if (e.IsSelected) {
            // Update form
            //...
        }
    }

这篇关于列表视图ItemSelectionChanged闪光两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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