Solved

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

Posted on 2012-04-12
2
261 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

728 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