Events

  • Thread starter Thread starter Tom Bean
  • Start date Start date
T

Tom Bean

Can events be used to notify an object that something has happened and it
needs to take some action?

Most of the documentation on events makes them sound like they work the
other way around, that is, an object raises an event to inform other objects
that something happened. I need the ability for many objects to notify a
single object that some event has occurred. e.g. similar to LostFocus.

In C++, I would have used SendMessage() to inform the object that it needs
to take some action but I can't find anything in C# to accomplish the same
thing.

Thanks,
Tom
 
Well, the object you want notified by the other objects could have a public
Notify() method. You could implement it asynchronously, if you need it to
return immediately. Notify() could also take a parameter of this, indicating
which caller called it...
 
If the event you need isn't already provided by the control (something like
Leave) I don't see how you could avoid writing code in each of these other
controls to call the notify event, or function, or whatever you use to
implement it. So you're saying that you have to subclass all these controls
because the events already given don't do what you need? I would agree with
the last two posters. Either make each of your controls call a public method
on the notifying control, with some sort of identification of the sender, or
make a new event delegate and add that event to each of the notifying
controls, let the class receiving the notification subscribe to each of
those events, and then add logic in each of the controls with the events to
fire the event when appropriate.

Chris
 
Chris,

I did as you and the others recommended and implemented a public method that
the other controls can call to notify the control about the event. After
thinking about it, I suppose that's not any different than using
SendMessage() in C++ which in the final analysis is just a call to a public
method.

Thanks,
Tom
 
Back
Top