You can't add an event handler the way you've mentioned. However, you can
inherit from the ArrayList class and add an event which you can raise when
an item is added. Here's how:
Public Class ArrayListEx
Inherits ArrayList
Public Event ItemAdded(ByVal Index As Integer)
Public Overrides Function Add(ByVal value As Object) As Integer
Dim ReturnValue As Integer = MyBase.Add(value)
RaiseEvent ItemAdded(ReturnValue)
Return ReturnValue
End Function
End Class
Private WithEvents oArr As New ArrayListEx
Private Sub TestSub()
oArr.Add(123)
oArr.Add("test")
End Sub
Private Sub oArr_ItemAdded(ByVal Index As Integer) Handles oArr.ItemAdded
MessageBox.Show("Index of Item Added: " & Index)
End Sub
Now just call TestSub from anywhere and you'll see a message box pop up
whenever an item is added to the ArrayList.
hope that helps..
Imran.
"Peter" <pe***@mclinn.com> wrote in message
news:dc**************************@posting.google.c om...
I have looked high and low for an example of adding an eventhandler to
an arraylist. I want a method to fire every time a item is added to
an arraylist. Any sample code would be appreciated.
IE. Dim myArrayList as new Arraylist
myArrayList.add("StringData") ---------->EventFires
Msgbox("A Record has been added.")
Thanks,
Peter