Array Element Changed Event - How?

  • Thread starter Thread starter JonathanG
  • Start date Start date
J

JonathanG

All,

Thanks in advance for any help. I am attempting to write a property
that is an array of strings. I am trying to trap the event when one of
the strings in the array changes.

When I step through the code, I can walk through the Set statement when
the length of the array changes (and therefore trap anything I want),
but if one of the elements change, it does not go through the
statement. I am attempting to write my own control from scratch, so I
would like to be able to edit the Labels property on the property grid.
I can do so now, but like I said, I cannot trap the event when a data
element changes. Here is my code:

Dim m_Labels() As String

Property Labels() As String()
Get
Return m_Labels
End Get
Set(ByVal Value As String())
m_Labels = Value
RaiseEvent Changed()
End Set
End Property

Can anyone help me determine how to raise the event 'Changed' if the
value of one of the elements of the array changes?

Thanks!
 
You need to declare a public event

Public Event MyEvent( sender as object, e as eventargs)

. . .

In your code

Raiseevent MyEvent( yourSenderObject, yourEventArgs )

.. . . . .

In the receiving class.

Private Withevents MyClass1 as MyClass

private sub MyEvent1_MyEvent( sender as object, e as eventargs ) Handles
MyClass1.Changed

HTH
 
Dear Jonathan,

I don't know the exactual implementation, however, I do think you could bind
your array and listen for the binder's changed event.
This would be sort of the same like when you do data binding, instead you
now use an array. If I am not mistaken, the binding class that achieves this
binding has change-events that you can use. I am not quite sure however.

I hope this helps you with answering your question,

Michel van den Berg
 
In regards to Mr. Newbie's response: The idea works, but only for
classes I define or inherit from. I want an event to fire when a
string changes. I cannot inherit from this 'Class'

RE: Michel: Can you elaborate, or point me in the direction of how
databinding is accomplished?

Thanks!
 
Dear Jonathan,

after looking further into it, I have to inform you that the way I described
is not a very good one. What you should do IMO is create your own collection
and 'expose' the OnSet functionality. For example, you could raise your own
custom made OnSet event when the (protected) OnSet function of the
collection is called. I, myself, would probally implement from
collectionbase
(http://msdn.microsoft.com/library/d...systemcollectionscollectionbaseclasstopic.asp).
Note: There is an example on that page. Replace Int16 with String and raise
your event like I said before.

Michel van den Berg
 
Back
Top