是否有触发一个ListView变化ListViewItems的,​​如果数字的事件? (Windows窗体) [英] Is there an event that triggers if the number of ListViewItems in a ListView changes? (Windows Forms)

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

问题描述

我想基于许多项目是如何在我的的ListView 控制开启/关闭一些其他控件。我找不到会做这无论如何,不​​管是在的ListView 本身或在 ListViewItemCollection 。也许有一种方法来观看一般在C#中收集的任何更改?

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?

我很乐意与其他事件也一样,甚至是那些有时火的时候,项目不会改变,但是例如 ControlAdded 布局事件没有工作:(

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.

当添加和删除的东西,而不是调用.items.add和items.remove,你​​叫你的其他函数的另一个解决方案可能是延长的ListView和。它仍然有可能增加,并没有被引发的事件中删除,但有一点code审查,以确保.items.add和.items.remove没有直接调用,它可以工作的结果很不错。这里有一个小例子。我只显示1添加功能,但也有6你将不得不实施,如果你想拥有使用所有可用的附加功能。还有.AddRange和.Clear你可能想看一看。

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

这篇关于是否有触发一个ListView变化ListViewItems的,​​如果数字的事件? (Windows窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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