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

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

[{'answer_id': 47178, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 0, 'selected': False, 'text': "<p>I can't find any events that you could use.  Perhaps you could subclass ListViewItemCollection, and raise your own event when something is added, with code similar to this.</p>\n\n<pre><code>Public Class MyListViewItemCollection\n    Inherits ListView.ListViewItemCollection\n\n    Public Event ItemAdded(ByVal Item As ListViewItem)\n\n    Sub New(ByVal owner As ListView)\n        MyBase.New(owner)\n    End Sub\n\n    Public Overrides Function Add(ByVal value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem\n        Dim Item As ListViewItem\n\n        Item = MyBase.Add(value)\n\n        RaiseEvent ItemAdded(Item)\n\n        Return Item\n    End Function\nEnd Class\n</code></pre>\n"}, {'answer_id': 47196, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 0, 'selected': False, 'text': '<p>I think the best thing that you can do here is to subclass ListView and provide the events that you want.</p>\n'}, {'answer_id': 47200, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 3, 'selected': True, 'text': "<p>@Domenic</p>\n\n<p>Not too sure, Never quite got that far in the thought process. </p>\n\n<p>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.</p>\n\n<pre><code>Public Class MonitoredListView\n    Inherits ListView\n\n    Public Event ItemAdded()\n    Public Event ItemRemoved()\n\n    Public Sub New()\n        MyBase.New()\n    End Sub\n\n    Public Function AddItem(ByVal Text As String) As ListViewItem\n        RaiseEvent ItemAdded()\n\n        MyBase.Items.Add(Text)\n    End Function\n\n    Public Sub RemoveItem(ByVal Item As ListViewItem)\n        RaiseEvent ItemRemoved()\n\n        MyBase.Items.Remove(Item)\n    End Sub\n\nEnd Class\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/47169', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3191/']