Solved

Adding an event to list(Of T)

Posted on 2013-10-22
6
202 Views
Last Modified: 2013-10-30
Is there a means to add an event to generic.list(Of T)?
0
Comment
Question by:Alyanto
  • 3
  • 2
6 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 39590595
Yes, but you will have to create your own custom class to handle the events...

For example, this code will show a message box with the newly added item when the button is clicked.
Public Class Form1

    Private WithEvents myList As MyCustomList

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        myList = New MyCustomList

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myList.Add("new item")
    End Sub

    Private Sub myList_ItemAdded(ByRef sender As System.Collections.Generic.List(Of String), ByRef item As String) Handles myList.ItemAdded
        MessageBox.Show("New item added: " & item)
    End Sub

End Class

Public Class MyCustomList
    Inherits Generic.List(Of String)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Event ItemAdded(ByRef sender As List(Of String), ByRef item As String)

    Public Overloads Sub Add(item As String)
        MyBase.Add(item)
        RaiseEvent ItemAdded(Me, item)
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:Alyanto
ID: 39591082
Is there a way to make this more generic, I can see from the code that it will only be a list of string.

Would it be possible to
                 
 Public Class MyCustomList
    Inherits Generic.List(Of T)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Event ItemAdded(ByRef sender As List(Of String), ByRef item As String)

    Public Overloads Sub Add(item As String)
        MyBase.Add(item)
        RaiseEvent ItemAdded(Me, item)
    End Sub

End Class

Open in new window

perhaps?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39591569
Can you describe what you are trying to achieve?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:Alyanto
ID: 39593352
In the very simplest sense I am trying to improve generic.list or similar so that when something is added or deleted from the list an event fires.  

This is a generic piece of code with no specific goal but has a general application.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39594525
right. Does following work in making it generic?

 Public Class MyCustomList
    Inherits Generic.List(Of T)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Event ItemAdded(ByRef sender As List(Of T), ByRef item As T)

    Public Overloads Sub Add(item As T)
        MyBase.Add(item)
        RaiseEvent ItemAdded(Me, item)
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 39611508
Excellent, thank you.

/Aly
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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