Solved

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

Posted on 2012-04-12
2
257 Views
Last Modified: 2012-04-15
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
Comment
Question by:andreas_rafn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 37837756
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
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 250 total points
ID: 37837770
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question