Handling Events with Delegates

Objects in C# can also support events . Events were created for graphical user interfaces (GUIs) like Windows so the user can direct program execution. When something happens in a GUIthe user clicks a button, moves a scroll bar, or closes a windowan event occurs. GUI code is written to respond to events, rather than simply to execute huge monolithic code blocks without user interaction. When code responds to events, the user directs the action and the program responds.

We're going to work with events in detail when creating Windows and Web applications, because events are designed for GUI environments. However, because you can handle events in C# with delegates, we'll get an introduction to the topic here. In C#, an object can publish an event and other objects can subscribe to that event so they'll be notified when the event has occurred.

When you subscribe to an event, you use a delegate to indicate which method you want to call when the event occurs. Here's an example; say you create a Button class that will display a button, giving this class a name field to hold the name of the button:

 
 public class Button {   public string name = "Button 1";   .   .   . } 

You can give this class an event named OnClick that will occur when the button is clicked, and a delegate that will be in charge of calling all code that subscribes to the OnClick event:

 
 public class Button {   public string name = "Button 1";  public delegate void ButtonClickDelegate(object button, string text);   public event ButtonClickDelegate OnClick;  .     .     . } 

You declare an event like OnClick with the event statement:

 
 [  attributes  ] [  modifiers  ] event  type declarator  ; [  attributes  ] [  modifiers  ] event  type member-name  {  accessor-declarations  }; 

Here are the parts of this statement:

  • attributes (Optional) Hold additional declarative information, as we'll see in Chapter 14.

  • modifiers (Optional) Optional modifiers include abstract , new , override , static , virtual , extern , or one of the four access modifiers.

  • type The delegate of this event.

  • declarator The name of the event.

  • member-name The name of the event.

  • accessor-declarations (Optional) Declaration of the accessors, which are used to add and remove event handlers in client code (although we're not going to use accessors in this example). The accessors are add and remove .

We'll also add a method named Click to the Button class, which will fire the OnClick event (that is, make the OnClick event occur), giving you some way of making the OnClick event happen:

 
 public class Button {   public string name = "Button 1";   public delegate void ButtonClickDelegate(object button, string text);   public event ButtonClickDelegate OnClick;  public void Click()   {   OnClick(this, "Clicked");   }  } 

Other code can use this delegate to subscribe to this event and so be called when the event occurs. To see how that works, take a look at the ButtonHandlerClass class. This class subscribes to button events when you pass a Button object to its Subscribe method. To subscribe to an event, you just add your own delegate to the delegate in the Button class, like this:

 
 public class ButtonHandlerClass {  public void Subscribe(Button button)   {   button.OnClick += new Button.ButtonClickDelegate(ButtonHandler);   }  .   .   . } 

The delegate we've added to the delegate in the Button class is for the ButtonHandler method, which means that method will be called when the button's OnClick event occurs. We can display a message in the ButtonHandler method indicating that it was notified of the event:

 
 public class ButtonHandlerClass {   public void Subscribe(Button button)   {     button.OnClick += new Button.ButtonClickDelegate(ButtonHandler);   }  public void ButtonHandler(object button, string text)   {   System.Console.WriteLine("{0} reports: {1}",   ((Button) button).name, text);   }  } 

At this point, the code is ready to go. To subscribe to a button's OnClick event, you just pass the Button object to a ButtonHandlerClass object's Subscribe method:

 
 Button button = new Button(); ButtonHandlerClass buttonHandler1 = new ButtonHandlerClass(); buttonHandler1.Subscribe(button); 

This subscribes buttonHandler1 to the button's OnClick event. When you call the button's Click method (which is how we fire the OnClick event in this example), the buttonHandler1 object's Buttonhandler method is called.

You can subscribe as many objects to the button's OnClick event as you want, as you see in ch04_18.cs, Listing 4.18, where two objects subscribe to that event. When the button's OnClick event fires, the event handlers that have subscribed to that event will be called, making them display a message.

Listing 4.18 Events and Delegates (ch04_18.cs)
 public class ch04_18 {   public static void Main()   {  Button button = new Button();   ButtonHandlerClass buttonHandler1 = new ButtonHandlerClass ();   buttonHandler1.Subscribe(button);   ButtonHandlerClass buttonHandler2 = new ButtonHandlerClass ();   buttonHandler2.Subscribe(button);   button.Click();  } } public class Button {   public string name = "Button 1";   public delegate void ButtonClickDelegate(object button, string text);   public event ButtonClickDelegate OnClick;   public void Click()   {     OnClick(this, "Clicked");   } } public class ButtonHandlerClass {   public void Subscribe(Button button)   {     button.OnClick += new Button.ButtonClickDelegate(ButtonHandler);   }   public void ButtonHandler(object button, string text)   {     System.Console.WriteLine("{0} reports: {1}",       ((Button) button).name, text);   } } 

Here's what you see when you run ch04_18.cs, shown in Listing 4.18. As you can see, both subscribed objects were notified of the OnClick event:

 
 C:\>ch04_18 Button 1 reports: Clicked Button 1 reports: Clicked 

That's it for this chapter. In Chapter 5, "Working with C# Streams," we're going to work with I/O in C# when we take a look at data streams.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

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