ArrayList Supports Notifications When Items Added or Removed



Public Class ArrayListChangedEventArgs
    Inherits EventArgs

    Public Sub New(list As CairoArrayList, action As ChangeAction)
        Me.List = list
        Me.Action = action
    End Sub

    Public Property List As CairoArrayList
    Public Property Action As ChangeAction

End Class ' ArrayListChangedEventArgs




Public Enum ChangeAction
    Add
    Remoce
End Enum




Public Class CairoArrayList

    Private _list As ArrayList

    Public Sub New()
        Me._list = New ArrayList
    End Sub

    Public ReadOnly Property Items As ArrayList
        Get
            Return Me._list
        End Get
    End Property

    Public Sub Add(value As Object)
        If Me._list IsNot Nothing AndAlso (Not Me._list.Contains(value)) Then
            Me._list.Add(value)
            Me.OnChanged(Me, New ArrayListChangedEventArgs(Me, ChangeAction.Add))
        End If
    End Sub

    Public Sub Remove(value As Object)
        If Me._list IsNot Nothing AndAlso (Me._list.Contains(value)) Then
            Me._list.Remove(value)
            Me.OnChanged(Me, New ArrayListChangedEventArgs(Me, ChangeAction.Remoce))
        End If
    End Sub

    Public Function Contains(key As Object) As Boolean
        Return Me._list IsNot Nothing AndAlso Me._list.Contains(key)
    End Function

    Protected Sub OnChanged(sender As Object, e As ArrayListChangedEventArgs)
        RaiseEvent Changed(Me, e)
    End Sub

    Public Event Changed As EventHandler(Of ArrayListChangedEventArgs)

End Class ' CairoArrayList

Usage:


Define CairoArrayList
Dim list As CairoArrayList = New CairoArrayList

Fill the list with data
list.Add("Cairo")
list.Add("Alex")
list.Add("Aswan")

Add Handler

AddHandler list.Changed, AddressOf OnListChanged

Do something when list changed
    Private Sub Onlistchanged(sender As Object, e As EventArgs)
        ' Do something when items dynamically removed or added to the list
    End Sub


Comments

Popular posts from this blog

Image Transition in VB.NET Windows Forms

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+