ListViewItems在ListView中的数量是否有变化? (Windows窗体) [英] Is there an event that triggers if the number of ListViewItems in a ListView changes? (Windows Forms)

查看:110
本文介绍了ListViewItems在ListView中的数量是否有变化? (Windows窗体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的 ListView 控件中的多少项启用/禁用其他控件。我无法找到任何这样的事情,无论是在 ListView 本身还是在 ListViewItemCollection 上。也许有一种方法可以在C#中一般性地收看任何收藏?



我也会喜欢其他的事件,更改,但例如 ControlAdded 布局事件无效:(。

解决方案

@Domenic



不太确定,在思想过程中绝对不会那么远/ p>

另一个解决方案可能是扩展ListView,当添加和删除东西,而不是调用.items.add和items.remove时,你调用其他函数仍然可以添加和删除而不会引发事件,但是通过一些代码审查来确保.items.add和.items.remove没有被直接调用,它可以很好地工作,这里有一个例子,我只是显示1个添加功能,但是有6个你必须实现,如果你想使用所有可用的添加功能,还有.Ad dRange和.Clear,你可能想看看。

 公共类MonitoredListView 
继承ListView

公共事件ItemAdded()
公共事件ItemRemoved()

Public Sub New()
MyBase.New()
End Sub

公共函数AddItem(ByVal Text As String)As ListViewItem
RaiseEvent ItemAdded()

MyBase.Items.Add(Text)
结束函数

Public Sub RemoveItem(ByVal Item As ListViewItem)
RaiseEvent ItemRemoved()

MyBase.Items.Remove(Item)
End Sub

结束类


I'd like to enable/disable some other controls based on how many items are in my ListView control. I can't find any event that would do this, either on the ListView itself or on the ListViewItemCollection. Maybe there's a way to generically watch any collection in C# for changes?

I'd be happy with other events too, even ones that sometimes fire when the items don't change, but for example the ControlAdded and Layout events didn't work :(.

解决方案

@Domenic

Not too sure, Never quite got that far in the thought process.

Another solution might be to extend ListView, and when adding and removing stuff, instead of calling .items.add, and items.remove, you call your other functions. It would still be possible to add and remove without events being raised, but with a little code review to make sure .items.add and .items.remove weren't called directly, it could work out quite well. Here's a little example. I only showed 1 Add function, but there are 6 you would have to implement, if you wanted to have use of all the available add functions. There's also .AddRange, and .Clear that you might want to take a look at.

Public Class MonitoredListView
    Inherits ListView

    Public Event ItemAdded()
    Public Event ItemRemoved()

    Public Sub New()
        MyBase.New()
    End Sub

    Public Function AddItem(ByVal Text As String) As ListViewItem
        RaiseEvent ItemAdded()

        MyBase.Items.Add(Text)
    End Function

    Public Sub RemoveItem(ByVal Item As ListViewItem)
        RaiseEvent ItemRemoved()

        MyBase.Items.Remove(Item)
    End Sub

End Class

这篇关于ListViewItems在ListView中的数量是否有变化? (Windows窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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