Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

How to create event handling for the change in generic collections in VB.NET?

I'm creating a user control that acts as a menu of registrations (the kind is irrelevant).
The user control is to have a collection of registrations as a property and the menu should mimic the changes in the collection.

So what I want is to somehow catch the event that the collection is changed.
This is easy when for the set property part (setting the entire collection). However it should also be possible to add individual registrations to the collection using the get part of the property, e.g. UserControl.Registrations.Add(Id,Registration), but I dont know how to catch such event.

Heres a simple code draft:

Public Class RegistrationHistoryMenu
    Private _Registrations As New Dictionary(Of Integer, Registration)
...
    Public Property Registrations As Dictionary(Of Integer, Registration)
        Get
            Return _Registrations
            'How do I catch changes when collection is read and then manipulated??
        End Get
        Set(Registrations As Dictionary(Of Integer, Registration))
            _Registrations = Registrations
            UpdateContent() 'Creting menu items according to collection
        End Set
    End Property
...
End Class

Open in new window

0
andreas_rafn
Asked:
andreas_rafn
2 Solutions
 
PaulHewsCommented:
This shows how to inherit from dictionary to accomplish what I think you want.  I'm using a form instead of a user control, but hopefully you'll see that the key is in the Registrations class, which inherits from dictionary and exposes an event that is raised when items are added.

Public Class Form1
    Private WithEvents x As New Registrations

    
    Private Sub x_ItemAdded() Handles x.ItemAdded
        MsgBox("Handle here")
    End Sub
    Private Class Registrations
        Inherits Dictionary(Of Integer, String)
        Public Event ItemAdded()

        Public Shadows Sub Add(Key As Integer, Value As String)
            RaiseEvent ItemAdded()
            MyBase.Add(Key, Value)
        End Sub

    End Class


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        x.Add(1, "hi")
    End Sub
End Class

Open in new window

0
 
nepaluzCommented:
I think you need to add your own method which you can then use to add to the class and raise the event too
Public Class RegistrationHistoryMenu
    Inherits Generic.Dictionary(Of Integer, Registration)
    Public Event Added(ByVal x As Integer, y As Registration)
    Sub AddedX(ByVal x As Integer, y As Registration)
        Me.Add(x, y)
        RaiseEvent Added(x, y)
    End Sub
End Class

Open in new window

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now