i want to detect if listview has been updated

eg:
there are listview1,listview2 and a button1 .u can send items to listview2 from listview1 .if the listview2 has an item button1 will enable if the listview2 is empty button1 will disable

what is the suitable event for this??
(there is an event called BindingContextChanged is it the suitable event?if so how to use it??)


*sorry about my english .i hope u will understand my question :$

Unhnd_Exception commented: Nice Response. -2
Member Avatar for Unhnd_Exception

Not sure if there is a binding for the items count.

You can just check the list views items count whenever you make a change and disable or enable the button. That would be pretty easy.

You can however create your own listview with an ItemChanged event.

This will look like overkill and it is if your only going to use it one time. But If you start making custom controls and keep adding to them then the future will allow you to drag and drop with all kinds of additional functionality.

I was board and decided to write this. I do have a ListViewPlus like the class below that has ItemMouseClick, Radio and Checkboxes, and more.

Theres really nothing special about the following. It shadows the Items Property to return the custom ListViewItemCollection. The Custom ListViewItem Collection will raise an Event whenever items change.

All you have to do is create a class named ListViewPlus and copy and paste the code into it. Rebuild the solution and the control will be in the toolbox. Drag it to the form and it will be identical to the listview except you will have an additional ItemChanged event. Put your code for enabling and disabling the button in the ItemChange Event.


Like I said I wrote this because I was bored. Use it or not.

The Form

Public Class Form1
   
    Private Sub ListViewPlus_ItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListViewPlus1.ItemChanged
        Button1.Enabled = ListViewPlus1.Items.Count > 0
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListViewPlus1.Items.Add("List View Plus")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListViewPlus1.Items.Clear()
    End Sub

End Class

The Extended ListView

Imports System.ComponentModel
Imports System.Windows.Forms.Design

Public Class ListViewPlus : Inherits ListView
    Public Event ItemChanged(ByVal sender As Object, ByVal e As EventArgs)

    Private ItemsWathced As ListViewPlusItemCollection

    Sub New()
        ItemsWathced = New ListViewPlusItemCollection(Me)
        AddHandler ItemsWathced.ItemChanged, AddressOf ListViewItemCollectionChanged
    End Sub

    Private Sub ListViewPlus_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleCreated
        RaiseEvent ItemChanged(Me, EventArgs.Empty)
    End Sub

    'Designer Serialization allows what you do on the designer to show up when 
    'you run the program.  The editor is for when your on the designer and you
    'look at the items property you will see (Collection) and will be an identical
    'designer when adding items to it as when you would a regular list view
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
     Editor(GetType(ListViewItemCollectionEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Shadows ReadOnly Property Items() As ListView.ListViewItemCollection
        Get
            Return ItemsWathced
        End Get
    End Property

    Private Sub ListViewItemCollectionChanged(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent ItemChanged(Me, EventArgs.Empty)
    End Sub

#Region "Custom Item Collection"

    'Inherits the ItemCollection and raises an event each time something changes
    'with the items.
    Public Class ListViewPlusItemCollection : Inherits System.Windows.Forms.ListView.ListViewItemCollection
        Public Event ItemChanged(ByVal sender As Object, ByVal e As EventArgs)

        Sub New(ByVal owner As System.Windows.Forms.ListView)
            MyBase.New(owner)
        End Sub

        Public Overrides Function Add(ByVal text As String) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Add(text)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function
        Public Overrides Function Add(ByVal text As String, ByVal imageIndex As Integer) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Add(text, imageIndex)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function
        Public Overrides Function Add(ByVal text As String, ByVal imageKey As String) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Add(text, imageKey)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function
        Public Overrides Function Add(ByVal key As String, ByVal text As String, ByVal imageIndex As Integer) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Add(key, text, imageIndex)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function
        Public Overrides Function Add(ByVal key As String, ByVal text As String, ByVal imageKey As String) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Add(key, text, imageKey)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function
        Public Overrides Function Add(ByVal value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Add(value)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function

        Shadows Sub AddRange(ByVal items() As ListViewItem)
            MyBase.AddRange(items)
            RaiseEvent ItemChanged(Nothing, Nothing)
        End Sub

        Public Overrides Sub Clear()
            MyBase.Clear()
            RaiseEvent ItemChanged(Nothing, Nothing)
        End Sub

        Public Overrides Function Insert(ByVal index As Integer, ByVal key As String, ByVal text As String, ByVal imageIndex As Integer) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Insert(index, key, text, imageIndex)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function
        Public Overrides Function Insert(ByVal index As Integer, ByVal key As String, ByVal text As String, ByVal imageKey As String) As System.Windows.Forms.ListViewItem
            Dim ListItem As ListViewItem = MyBase.Insert(index, key, text, imageKey)
            RaiseEvent ItemChanged(Nothing, Nothing)
            Return ListItem
        End Function

        Public Overrides Sub Remove(ByVal item As System.Windows.Forms.ListViewItem)
            MyBase.Remove(item)
            RaiseEvent ItemChanged(Nothing, Nothing)
        End Sub
        Public Overrides Sub RemoveAt(ByVal index As Integer)
            MyBase.RemoveAt(index)
            RaiseEvent ItemChanged(Nothing, Nothing)
        End Sub
        Public Overrides Sub RemoveByKey(ByVal key As String)
            MyBase.RemoveByKey(key)
            RaiseEvent ItemChanged(Nothing, Nothing)
        End Sub

    End Class

#End Region

#Region "Custom Item Collection Editor"

    'For designer use when adding items.
    Public Class ListViewItemCollectionEditor
        Inherits System.ComponentModel.Design.CollectionEditor

        Public Sub New(ByVal newType As Type)
            MyBase.new(newType)
        End Sub

        Protected Overrides Function CanSelectMultipleInstances() As Boolean
            Return False
        End Function

        Protected Overrides Function CreateCollectionItemType() As Type
            Return GetType(ListViewItem)
        End Function

    End Class

#End Region

End Class

<Edit>
I should say if you used this you would want to add multiple items with the AddRange sub. This will now have an event raised everytime you add an item and would perform worse than the regular listview. AddRange is better anyways and would have no affect with performance of the new event.

And the ItemChanged Event would be more meaningfull if it was renamed to ItemsCountChanged.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.