Is there an event that triggers if the number of ListViewItems in a ListView changes? (Windows Forms)

后端 未结 3 1612
一个人的身影
一个人的身影 2021-01-20 20:39

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

3条回答
  •  执念已碎
    2021-01-20 21:31

    @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
    

提交回复
热议问题