5.8 Defining and Registering Event Handlers

 <  Day Day Up  >  

You want to consume the events that are fired from an object.


Technique

Define a method that has the same method signature as the delegate to which an event is bound to. It becomes the event handler. To register this handler to receive events when they are fired, use the addition assignment operator ( += ) on the event name of the object instance you want to subscribe to and assign that to an instance of a bound delegate passing the event-handler name to the delegate constructor, as shown in the Main method of Listing 5.5.

Listing 5.5 Creating Event Handlers
 class Class1 {     [STAThread]     static void Main(string[] args)     {         ConsoleMenu menu = new ConsoleMenu();         menu.AddMenuItem( "Item 1" );         menu.AddMenuItem( "Item 2" );         menu.AddMenuItem( "Item 3" );         menu.AddMenuItem( "Quit" );         menu.OnMenuEvent += new             ConsoleMenu.MenuEventHandler(MenuItemHandler);         menu.DisplayMenu();     }     static void MenuItemHandler( object sender, string[] args )     {         switch( Int32.Parse( args[0].ToString() ))         {             case( 1 ):             case( 2 ):             case( 3 ):             {                 Console.WriteLine( "You selected the item {0}\n", args[1] );                 break;             }             case( 4 ):             {                 Console.WriteLine( "Goodbye!" );                 ((ConsoleMenu)sender).QuitMenu();                 break;             }         }     } } 

Comments

Listing 5.5 shows how to subscribe to the events that are fired from the ConsoleMenu class in Listing 5.4. When binding a method implementation to a delegate, you create the method using the same signature as the delegate, and with events, this process is the same. The delegate in Listing 5.4 contains two parameters, an object and an array of strings, and the method MenuItemHandler in Listing 5.5 follows that same convention.

Instead of binding a method to a delegate, however, event handlers are bound to the actual event name defined within the class that fires that event. You do so using the addition assignment operator ( += ), which assigns the event to an instance of its delegate class. If you compare this step to the delegate recipe earlier in this chapter, you should see some peculiar similarities. Earlier, a variable whose data type was that of the generated delegate class was created within the client who was using the delegate. In Listing 5.4, that variable is declared within the class that fires the event, and the event keyword is added to it. Furthermore, just as you bind a method to a delegate using the new keyword passing the name of the method within the constructor, the same process occurs with events and the name of the event handler is passed as the delegate constructor.

At this point, the event communication between a class and a client is set. The event that is implemented by the class which fires it is bound to an event handler. Therefore, when the class that fires the event calls the corresponding event method, the runtime routes the call to the method handlers that have subscribed to that event. It is there that processing can occur based on the arguments passed into the event handler. Within the event handler of Listing 5.5, you can see that the menu is dismissed by calling the QuitMenu method on the ConsoleMenu instance. This method sets the Boolean value quitMenu that is used to control the display of the menu. Figure 5.5 shows the result of running the ConsoleMenu class and its associated client code.

Figure 5.5. Using events to create a simple console-based menu system.

graphics/05fig05.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net