Events

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); }

Events are one of the more interesting additions to VB .NET and are an integral part of the C# language and the .NET platform. The event mechanism provides a method of communication or notification between objects. Consider the Windows Forms Button control. When a mouse-click event occurs, the button notifies any subscriber that a Click event has occurred. The subscriber can then perform processing based on the event received from the publisher of the event.

Subject-Observer

The event paradigm is an implementation of the Subject-Observer pattern. In the Subject-Observer model, also known as Publish-Subscribe, the Subject notifies registered Observers when the state of the Subject has changed. Figure 2.5 shows a UML, or Unified Modeling Language, diagram of the Subject-Observer pattern classes.

Figure 2.5. The Subject-Observer pattern.

figure 2.5. the subject-observer pattern.

The Subject-Observer is a very useful design pattern and can be easily implemented in both C# and VB .NET. In addition, it is often helpful to understand the fundamental concepts when dealing with a high-level abstraction such as events. The event model used in .NET reduces the dependency on having known methods for a subject and observer as in the Subject-Observer pattern. Rather than requiring an Observer to provide a known method such as Update, the .NET event model allows for loosely coupled events and event handlers.

Delegates

To provide a loosely coupled event system, .NET introduces the concept of delegates. For C/C++ programmers, delegates are like function pointers on steroids. In VB parlance, delegates are similar to passing the address of a function to another function using the AddressOf operator. For an object to subscribe to an event, it need only implement a member method with the required delegate signature, the signature being the return type and parameters for the method.

As an example, consider the Click method on a Button control. To handle the event, a method must exist that accepts two parameters: object sender and EventArgs e. As long as the member method conforms to this method signature, that method can be used to attach to the published event. Events and delegates go hand in hand as delegates are used by events to invoke the specified method.

Defining Events

To expose an event, a class needs to define the event within the class declaration. Listing 2.4 shows an example of defining an event.

Listing 2.4 Declaring a Public Event
  1: public class MyControl {  2:  3:     public event System.EventHandler       MyEvent;  4:  5:  6:     protected virtual void OnMyEvent( EventArgs e ) {  7:           if( MyEvent != null )  8:               MyEvent( this, e );  9:     } 10:  } 

The MyEvent member of the MyControl class defines an event with a delegate type of System.EventHandler and, as recommended by the coding standards, implements a protected virtual method OnMyEvent to handle dispatching the event. The reason for the protected virtual method is so that derived classes can have a first crack at the event and can decide whether the event should be propagated to all registered listeners.

The MyControl class uses the method OnMyEvent internally to raise the event. Anytime it is necessary to invoke the event, code within the MyControl class would merely make a call to the protected OnMyEvent method, passing it a new instance of the EventArgs type. In turn, the MyEvent member is tested to determine whether it is null, and if not, all observers of this event are notified.

In addition to using predefined delegate types, you can create your own delegate signatures. Listing 2.5 provides for a custom delegate signature and EventArgs derived class.

Listing 2.5 Custom Delegate/Event
  1: public class MyEventArgs : EventArgs {  2:    //MyEventArgs implementation  3: }  4:  5: public delegate void MyEventHandler( object sender, MyEventArgs e );  6:  7:  8: public class MyControl {  9: 10:     public event MyEventHandler    MyEvent; 11: 12:     protected virtual void OnMyEvent( MyEventArgs e ) { 13:           if( MyEvent != null ) 14:               MyEvent( this, e ); 15:     } 16: } 17: 18: 19: 20: 21: public class MyForm { 22: 23:    private MyControl myControl; 24: 25: 26:    public void InitializeComponent( ) { 27: 28:         myControl = new MyControl( ); 29:         myControl.MyEvent += new MyEventHandler( this.OnMyEvent ); 30:    } 31: 32: 33:    public void OnMyEvent( object sender, MyEventArgs e ) { 34:        //TODO: Handle the event 35:  } 36: } 

Listing 2.5 creates a custom EventArgs derived class used by the custom MyEventHandler delegate that is then consumed by a client declaring a method whose signature matches that of the MyEventHandler delegate.



    .NET Windows Forms Custom Controls
    User Interfaces in VB .NET: Windows Forms and Custom Controls
    ISBN: 1590590449
    EAN: 2147483647
    Year: 2002
    Pages: 74

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