Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-12
2
Medium Priority
?
263 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 1000 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 1000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 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