Extending OpenNETCF.Windows.Forms.Button2 class

March 10, 2006 — Leave a comment

The OpenNETCF Smart Device Framework 2.0 is currently in Beta1 and is currently released in binary form only during the beta cycle.  Even though it’s currently available only in compiled mode you can still extend the functionality of controls.  For example, I’m working on a project where I’m using the OpenNETCF.Windows.Forms.Button2 and the Image property for the button.  There are a bunch of buttons that get enabled/disbled but unfortunately when the button is disabled it still looks disabled because of the image.


To resolve the problem we can inherit from the Button2 class.  We can create a class ExtendedButton (not the name I used in the project but used it for explanation purposes).  Here is the class implementation: 


/// <summary>
/// Provides the ability for enabled and disabled images
/// </summary>
public partial class ExtendedButton : Button2
{
   private Image disabledImage = null;
   private Image enabledImage = null;

   public ExtendedButton()
   {
      InitializeComponent();
   }

   /// <summary>
   /// Gets or sets the disabled Image
   /// </summary>
   public Image DisabledImage
   {
      get
      {
         return this.disabledImage;
      }
      set
      {
         this.disabledImage = value;
      }
   }

   /// <summary>
   /// Gets or sets the image. Hides the base class implementation but 
   /// internally still calls the base implementation
   /// </summary>
   public new Image Image
   {
      get
      {
         return base.Image;
      }
      set
      {
         this.enabledImage = value;
         base.Image = this.enabledImage;
      }
   }

   /// <summary>
   /// Hides the System.Windows.Forms.Control.Enabled property. This is
   /// where we set the Button2.Image property depending on the enabled
   /// status.
   /// </summary>
   public new bool Enabled
   {
      get
      {
         return base.Enabled;
      }
      set   
      {
         base.Enabled = value;
         if (base.Enabled)
            base.Image = this.enabledImage;
         else
         {
            if(this.disabledImage!=null)
            base.Image = this.disabledImage;
         }
         this.Invalidate();
      }
   }


 


Here is a screen shot of the sample application:



Sample application is also attached. 


Purpose of doing this is to show that you can extend controls without having the source available to you. This functionality wasn’t added to the SDF class because we are in somewhat of a code freeze for new features, but if there are enough request we can added it after the SDF2.0 release.

ExtendedButton.zip (15.35 KB)

No Comments

Be the first to start the conversation.

Leave a Reply