NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Defining an Event

For an overview of how to create a custom event, see Providing Event Functionality. If the event that your control will raise does not have any associated event data, you can use the base class System.ComponentModel.EventArgs for the event "data". In such a case, you do not have to declare a delegate class, but can use the System.ComponentModel.EventHandler as your event handler. All that remains to be done is to provide public AddOn<EventName> and RemoveOn<EventName> methods that attach a delegate or remove a delegate from your event, and a protected On<EventName> method that raises the event.

The following code fragment shows how event functionality for a custom event, ValueChanged, is provided in the control FlashTrackBar. The complete code for the FlashTrackBar sample is provided in Win Forms Control Sample.

[C#]
public class FlashTrackBar : RichControl {

//The event does not have any data, so EventHandler is adequate 
//as the event delegate.
private EventHandler valueChanged;
        //
        // <doc>
        // <desc>
        //Adds an event handler for the value changed event.  This 
        //event is raised when the value of the track bar changes.
        // </desc>
        // </doc>
        //
        public void AddOnValueChanged(EventHandler handler) {
            valueChanged = (EventHandler)Delegate.Combine(valueChanged, handler);
        }
        
        //
        // <doc>
        // <desc>
        // The protected method of our value changed event.  This is the
        // method that raises the event when the value has actually 
        // changed. Deriving controls may override this.  
        // </desc>
        // </doc>
        //
        protected virtual void OnValueChanged(EventArgs e) {
            if (valueChanged != null) {
                valueChanged(this, e);
            }
        }
//
        // <doc>
        // <desc>
        //      Removes the given event handler.
        // </desc>
        // </doc>
        //
        public void RemoveOnValueChanged(EventHandler handler) {
            valueChanged = (EventHandler)Delegate.Remove(valueChanged, handler);
        }
}