The main functional requirement of the OutlookBar control is that the control will host other controls within the OutlookBarTab components. Each new child control will be associated with a particular tab within the OutLookBar, and that child control will be activated when its parent tab is selected. Figure 8.1 shows the OutlookBar control hosting a TreeView control and a Panel control. Figure 8.1. The OutlookBar control in action.Because the OutlookBar control will act as a container control for other .NET controls, the emphasis of this chapter is to demonstrate the implementation of this particular requirement. Extending the OutlookBar controls feature set will be left to you as an exercise. Before the discussion dives into the code for the control, the subject of defining custom events needs to be discussed. The following section discusses custom events. Custom EventsThe OutlookBar control defines a custom event and an associated event handler. In Windows Forms development, controls raise events to notify the observer either of some change in the state of the control or that an action has occurred that should be responded to. Generally, event handlers for such events take the following form: access-modifier void OnControl-Name_Event( object sender, EventArgs e ) The EventArgs parameter can be the System.EventArgs class or a class that derives from EventArgs, such as PaintEventArgs or MouseEventArgs. Defining custom events and event handlers, known as delegates, provides a clear message to the user of the control as to the event type and the expected event arguments that pertain to the event. Listing 8.1 shows the proper semantic for defining custom events. Listing 8.1 Custom Events1: using System; 2: 3: namespace CustomEvents 4: { 5: 6: 7: public class MyCustomEventArgs : System.EventArgs { 8: 9: private string msg; 10: 11: public string Message { 12: get { return msg; } 13: } 14: 15: 16: public MyCustomEventArgs( string Message ) { 17: msg = Message; 18: } 19: } 20: 21: public delegate void MyCustomEventHandler( object sender, MyCustomEventArgs e ); 22: 23: 24: public class MyControl { 25: 26: public event MyCustomEventHandler CustomEvent; 27: 28: 29: public void RaiseEvent( string Message ) { 30: OnMyCustomEvent( new MyCustomEventArgs( Message ) ); 31: } 32: 33: protected virtual void OnMyCustomEvent( MyCustomEventArgs e ) { 34: if( CustomEvent != null ) 35: CustomEvent( this, e ); 36: } 37: 38: } 39: 40: 41: 42: 43: public class EventTest { 44: 45: public static void OnMyControl_CustomEvent( object sender, MyCustomEventArgs e ) { 46: Console.WriteLine( string.Format( "Caught custom event : { 0} ", e.Message ) ); 47: } 48: 49: public static void Main( ) { 50: 51: MyControl ctrl = new MyControl( ); 52: 53: ctrl.CustomEvent += new MyCustomEventHandler( EventTest.OnMyControl_CustomEvent ); 54: 55: ctrl.RaiseEvent( "Raising Event" ); 56: } 57: } 58: } The process of creating custom events generally begins by defining the event and the event parameters. According to the development guidelines provided by Microsoft, event handlers should define two parameters. The first parameter for an event is the sender or originator of the event. The second parameter is the necessary information about the event, and it should be an EventArgs derived class. Note Delegates are the basic mechanism for handling events and notifications within the .NET framework. Extending the basic set of events and defining custom events is a programming task that all .NET developers should become proficient in. Notice the method OnMyCustomEvent on line 33 of Listing 8.1. Defining a protected virtual event handler within the class allows for derived classes to override the event handler to perform any custom processing before allowing the event to propagate to the registered observers of the event. |