Chapter 7. Events
This chapter covers:
- Bubble-up events
- Tunnel-down events
- Handling events even when they’ve already been handled
- Class-level events
- Clever ways to annoy your users
If you’ve used both MFC and Windows Forms, you’ll know that the event model in Windows Forms was a major improvement over the message-map model used by MFC. Controls in Windows Forms exposed events that could be subscribed to by code that cares, and that code was called when appropriate. The classic example is a user clicking a button, resulting in the appropriate handler being called. Many other events work in the same way.
Classic Windows Forms events did have some issues. The most problematic was that the code that cared about the event either needed to have direct access to the event generator, or the event had to be manually passed up the chain. For example, picture a button on a user control on a form in an application. If the application needs to know about the event, the application either needs to know about the button (breaking encapsulation), or the button needs to tell the user control, which needs to tell the form, which needs to tell the application—which is a pain in the neck.