Friday, 18 July 2008

Create a custom label control

Code for a custom label control. This creates a control that inherits from the standard System.Windows.Forms.Label control, and adds the facility to make the background a colour gradient fill.

   1:  using System;
   2:  using System.Drawing;
   3:  using System.Drawing.Drawing2D;
   4:  using System.Windows.Forms;
   5:   
   6:  namespace MyNamespace
   7:  {
   8:      public class CustomLabel : Label
   9:      {
  10:          # region Fields
  11:   
  12:          private Color _col1;
  13:          private Color _col2;
  14:          private Color _col3;
  15:          private Color _col4;
  16:          private LinearGradientMode _grad;
  17:   
  18:          # endregion
  19:   
  20:          # region Properties
  21:   
  22:          public Color Colour1
  23:          {
  24:              get { return this._col1; }
  25:              set
  26:              {
  27:                  this._col1 = value;
  28:                  this.Invalidate();
  29:              }
  30:          }
  31:   
  32:          public Color Colour2
  33:          {
  34:              get { return this._col2; }
  35:              set
  36:              {
  37:                  this._col2 = value;
  38:                  this.Invalidate();
  39:              }
  40:          }
  41:   
  42:          public LinearGradientMode Gradient
  43:          {
  44:              get { return this._grad; }
  45:              set
  46:              {
  47:                  this._grad = value;
  48:                  this.Invalidate();
  49:              }
  50:          }
  51:   
  52:          protected override Size DefaultSize
  53:          {
  54:              get { return new Size(64, 16); }
  55:          }
  56:   
  57:          # endregion
  58:   
  59:          # region Constructors
  60:   
  61:          public CustomLabel()
  62:              : base()
  63:          {
  64:              this._col1 = Color.White;
  65:              this._col2 = Color.Blue;
  66:              this._col3 = _col1;
  67:              this._col4 = _col2;
  68:              this._grad = LinearGradientMode.Vertical;
  69:              this.DoubleBuffered = true;
  70:          }
  71:   
  72:          public CustomLabel(Color c1, Color c2, LinearGradientMode grad)
  73:              : base()
  74:          {
  75:              this._col1 = c1;
  76:              this._col2 = c2;
  77:              this._col3 = _col1;
  78:              this._col4 = _col2;
  79:              this._grad = grad;
  80:              this.DoubleBuffered = true;
  81:          }
  82:   
  83:          # endregion
  84:   
  85:          # region Overrides
  86:   
  87:          protected override void OnPaintBackground(PaintEventArgs pevent)
  88:          {
  89:              //base.OnPaintBackground(pevent);
  90:   
  91:              LinearGradientBrush lgb = new LinearGradientBrush
  92:                  (this.ClientRectangle, this._col1, this._col2, this._grad);
  93:              pevent.Graphics.FillRectangle(lgb, this.ClientRectangle);
  94:              lgb.Dispose();
  95:          }
  96:   
  97:          protected override void OnEnabledChanged(EventArgs e)
  98:          {
  99:              base.OnEnabledChanged(e);
 100:   
 101:              if (this.Enabled)
 102:              {
 103:                  this._col1 = this._col3;
 104:                  this._col2 = this._col4;
 105:              }
 106:              else
 107:              {
 108:                  this._col1 = Color.LightGray;
 109:                  this._col2 = Color.White;
 110:              }
 111:   
 112:              this.Invalidate();
 113:          }
 114:   
 115:          # endregion
 116:      }
 117:  }


When this code is created, the CustomLabel control will be available to add to forms from the control toolbox. Simply add it as per any other forms control.

Notes:

Colour1 & Colour2 properties: These are the properties that specify which colours to use for the gradient fill.

Gradient: This property specifies the type of gradient to use.

Constructors: The default constructor makes the gradient coloured from white to blue, whereas the parameterized constructor colours the label with the user specified colours.

OnPaintBackground: This method is the override for the method called when the background paint event is fired. Note that the call to the base method is inhibited, (i.e. commented out!) because it is not necessary as a new background is being painted.

OnEnabledChanged: When the .Enabled property of the control is changed, an event is fired which executes this method. Notice this time that the base method is called. The colour of the control is changed to light grey through white gradient when the control is disabled to give the appearance of being 'greyed out'.

0 comments: