How to capture Item's event through an object of its collection in a form?

  • Thread starter Thread starter hemant
  • Start date Start date
H

hemant

There are two classes
1) Key - contains some private variables, one method "Reset" and one
event "valuesReset" which is raised by Reset method.
2) Keys - this class is collection of objects of class 'Key' and it is
inherited with ReadOnlyCollection(Of key).
I'm pasting the code here

Class key
Public Event valuesReset()

Public _name As String = ""
Public _title As Integer = 0
Public _strong As Integer = 0
Public _italic As Integer = 0

Public Function reset() As Boolean
_name = ""
_title = 0
_strong = 0
_italic = 0

RaiseEvent valuesReset()
End Function
End Class

'Collection of key
Class keys
Inherits ReadOnlyCollection(Of key)

Friend Sub New()
MyBase.New(New List(Of key))
End Sub

Friend Function add(ByVal tmpKey As key) As Integer
If Items.Contains(tmpKey) Then
Return Items.IndexOf(tmpKey)
End If

Items.Add(tmpKey)
Return Count - 1
End Function

Friend Sub AddAt(ByVal tmpKey As key, ByVal index As
Integer)
If index < 0 OrElse index > Items.Count - 1 Then
Return
End If
If Contains(tmpKey) Then
Return
End If

Items.Insert(index, tmpKey)
End Sub

Friend Sub Dispose()
For i As Integer = Count - 1 To 0 Step -1
Me(i).reset()
Next
End Sub
Friend Sub remove(ByVal tmpKey As key)
Items.Remove(tmpKey)
End Sub
End Class

Class Form1
withevents objKeys as Keys

' I want to capture the "valuesReset" event here with
the index number of the key type
' bject

End Class

Now the problem is how can I capture the event "valuesReset" of an key
object if I use
"withevents objKeys as Keys" in any form/outside class? and if it is
possible how can I know which key object inside the collection has
raised the event? Thanks In advance :-)
 
Now the problem is how can I capture the event "valuesReset" of an key
object if I use
"withevents objKeys as Keys" in any form/outside class? and if it is
possible how can I know which key object inside the collection has
raised the event? Thanks In advance :-)

I think in order to achieve what you want, you will need to add an event to
your "Keys" class (e.g. "ItemReset"). In your add methods, you will need to
add an event handler to the "valuesReset" event of the "Key" object being
added. The event handler should then raise your new event (i.e. "ItemReset"),
using the "IndexOf" method to find the index of the Key that raised the
original event.

Incidentally, why are you inheriting from ReadOnlyCollection if you are
allowing items to be added to it?

HTH,

Mark
 
Back
Top