Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now