Control-Based Extender Providers

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); }

As stated previously, any component can serve as an Extender Provider. All that is necessary is to implement the IExtenderProvider interface, add the necessary ProvideProperty attributes, and implement the Get/Set methods for the property. Creating a control that implements the IExtenderProvider interface is the same as it was for the BorderPainterExtender sample described previously. To demonstrate this, the next Extender Provider will be derived from a Label control. In addition, this new Extender Provider will only extend MenuItems to provide a MenuItemHelpText property. Figure A.3 shows the completed MenuItemExtender displaying some help text for the selected MenuItem.

Figure A.3. A Label control implementing IExtenderProvider.

figure a.3. a label control implementing iextenderprovider.

The MenuItemExtender only provides the MenuItemHelpText property to MenuItems. This is accomplished in two ways: First, the target base class is specified in the ProvideProperty attribute by specifying typeof( MenuItem ). Second, the CanExtend method returns true only if the target is a MenuItem. The code for implementing the MenuItemExtender is simple, to say the least. Listing A.5 contains the implementation.

Listing A.5 MenuItemExtender Source
  1: [ ProvideProperty( "MenuItemHelpText", typeof( MenuItem ) ) ]  2: public class MenuItemExtender : Label, IExtenderProvider {  3:  4:     protected Hashtable        menuItemHelpText;  5:  6:     //Hide the base class's Text Property  7:     [Browsable(false)]  8:     public new string Text {  9:         get {  return base.Text; } 10:         set {  base.Text = value; } 11:     } 12: 13:     //Implement IExtenderProvider 14:     bool IExtenderProvider.CanExtend( object target ) { 15:         return (target is MenuItem); 16:     } 17: 18:     public MenuItemExtender( ) { 19:        menuItemHelpText = new Hashtable( ); 20:     } 21: 22:     //Provide Get/Set Methods for the MenuItemHelpText property 23:     public string GetMenuItemHelpText( object target ) { 24:         string text = string.Empty; 25:   if( menuItemHelpText.ContainsKey( target ) ) 26:             text = (string)menuItemHelpText[ target ]; 27:         return text; 28:     } 29: 30:     public void SetMenuItemHelpText( object target, string text ) { 31:         MenuItem mi = (MenuItem)target; 32:         if( text == null || (text.Length == 0) ) { 33:             menuItemHelpText.Remove( mi ); 34:             mi.Select -= new EventHandler( OnMenuItemSelect ); 35:         }  else { 36:             menuItemHelpText[ mi ] = text; 37:             mi.Select += new EventHandler( OnMenuItemSelect ); 38:         } 39:     } 40: 41:     protected void OnMenuItemSelect( object sender, EventArgs e ) { 42:         base.Text = (string)menuItemHelpText[ sender ]; 43:     } 44:  } 

Not unlike the BorderPainterExtender, the MenuItemExtender implements the IExtenderProvider interface to dynamically add a property to all MenuItem-derived objects on the current form. Any Component-derived class can implement the IExtenderProvider interface to provide dynamic properties to controls during design-time.

The MenuItemExtender associates simple help text with a MenuItem first by providing the dynamic property MenuItemHelpText and second by subscribing to the Select event for the given MenuItem. When the Select event occurs, the method OnMenuItemSelect is invoked. This method then assigns the associated help text with the Text property. Because the MenuItemExtender is derived from a Label, the Text will then be displayed.



    .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