In Depth Banner
Skip Navigation Links

Select your preferred language

The full LEDBar listing.

using System;

using System.ComponentModel;

using System.Collections;

using System.Drawing;

using System.Windows.Forms;

 

namespace WellFormed

{

  /// <summary>

  /// The LEDBar control displays numeric information using LEDControl style numerals

  /// </summary>

  public class LEDBar : Control

  {

    public enum LEDBarStyles

    {

      DecimalInteger,

      FixedPoint,

      FloatingPoint,

      Hexadecimal,

      Octal,

      Binary

    }

 

    int _digits; //The number of digits in the bar

    double _value; //The value to display

    bool _showinactivesegments;

    LEDBarStyles _style; //The numeric style of the bar.

    int _precision; //The doubleing point maximum precision

    bool _showsign=false; //When true, the display shows the numerical sign

 

    [

    Category("Appearance"),

    Description("Enables the sign display"),

    Browsable(true)

    ]

    public bool ShowSign

    {

      get{return _showsign;}

      set

      {

        _showsign=value;

        OnUpdateControl(EventArgs.Empty);

      }

    }

 

    [

    Category("Appearance"),

    Description("The doubleing point maximum precision"),

    DefaultValue(2),

    Browsable(true)

    ]

    public int Precision

    {

      get{return _precision;}

      set

      {

        _precision=value;

        OnUpdateControl(EventArgs.Empty);

      }

    }

    

    [

    Category("Appearance"),

    Description("The numeric style of the bar."),

    DefaultValue(LEDBarStyles.DecimalInteger),

    Browsable(true)

    ]

    public LEDBarStyles NumericStyle

    {

      get{return _style;}

      set

      {

        _style=value;

        InternalApplyStyle();

      }

    }

 

    [

    Category("Appearance"),

    Description("The number of digits in the bar"),

    DefaultValue(4),

    Browsable(true)

    ]

    public int Digits

    {

      get{return _digits;}

      set

      {

        _digits=value;

        InternalDigitsChanged();

        InternalSizeChanged();

        InternalApplyStyle();

        OnUpdateControl(EventArgs.Empty);

      }

    }

 

    [

    Category("Misc"),

    Description("The value to display"),

    Browsable(true)

    ]

    public double Value

    {

      get{return _value;}

      set

      {

        _value=value;

        OnUpdateControl(EventArgs.Empty);

      }

    }

 

 

    [

    Category("Appearance"),

    Description("When true, inactive segments are shown in an appropriate colour"),

    DefaultValue(false),

    Browsable(true)

    ]

    public bool ShowInactiveSegments

    {

      get{return _showinactivesegments;}

      set

      {

        _showinactivesegments = value;

        foreach(Control c in Controls)

        {

          if(c as LEDControl != null)

          {

            ((LEDControl)c).ShowInactiveSegments=value;

          }

        }

        OnUpdateControl(EventArgs.Empty);

      }

    }

 

 

    public LEDBar()

    {

      Digits = 4;

    }

 

 

    protected virtual void InternalSizeChanged()

    {

      int nc = 1;

      foreach(Control c in Controls)

      {

        if(c as LEDControl != null)

        {

          ((LEDControl)c).Location=new Point(Width - ((Width/Digits)*nc++),0);

          ((LEDControl)c).Size=new Size(Width/Digits,Height);

        }

      }

    }

 

    protected virtual void InternalDigitsChanged()

    {

      if(Digits==0)

        return;

      this.Controls.Clear();

      for(int x=0; x<Digits; x++)

      {

        LEDControl led = new LEDControl();

        this.Controls.Add(led);

      }

    }

 

     protected override void OnSizeChanged(EventArgs e)

    {

      InternalSizeChanged();

      OnUpdateControl(e);

    }

 

    protected virtual void OnUpdateControl(EventArgs e)

    {

      string formatstring;

      string s="";

      int cmp;

 

      switch(_style)

      {

        case LEDBarStyles.FloatingPoint:

          formatstring="G";

          s=Value.ToString(formatstring);

          break;

        case LEDBarStyles.FixedPoint:

          formatstring = string.Format("f{0}",Precision);

          s=Value.ToString(formatstring);

          break;

        case LEDBarStyles.Binary:         

          int b=(int)Value;

          cmp=1;

          for(int x=0;x<Digits;x++)

          {

            if((b&1) == 1)

              s="1"+s;

            else

            {

              if(!(cmp>(int)Value) || cmp==1)

                s="0"+s;

            }

            b/=2;

            cmp*=2;

          }

          break;

        case LEDBarStyles.DecimalInteger:

          formatstring="0";

          s=Value.ToString(formatstring);

          break;

        case LEDBarStyles.Hexadecimal:

          formatstring="X";

          s=((int)Value).ToString(formatstring);

          break;

        case LEDBarStyles.Octal:

          int d=(int)Value;

          cmp=1;

          for(int x=0;x<Digits;x++)

          {

            int t=d&0x07;

            if(!(cmp>(int)Value) || cmp==1)

              s=t.ToString()+s;

            d>>=3;

            cmp*=8;

          }

          break;

      }

 

      if(s=="")

        s=" ";

      if(s!=" " && s[0]!='-' && this.ShowSign)

        s="+"+s;

      if(s.Length>Digits+1)

        s=s.Substring(0,Digits+1);

      if(s.Length>Digits && (s.IndexOf('.')<0 || s.IndexOf('.')>=Digits))

        s="ERROR";

      int stringindex=s.Length-1;

      int digitindex=0;

      while(digitindex<Digits)

      {

        ((LEDControl)Controls[digitindex]).Decimal=false;

        if(stringindex>-1)

        {

          if(s[stringindex]=='.')

          {

            ((LEDControl)Controls[digitindex]).Decimal=true;

            stringindex--;

          }

          else

            ((LEDControl)Controls[digitindex]).Decimal=false;

 

 

          ((LEDControl)Controls[digitindex]).DisplayChar=s[stringindex--];

        }

        else

        {

          ((LEDControl)Controls[digitindex]).DisplayChar=' ';

        }

        digitindex++;

      }

      Invalidate();

    }

 

    protected virtual void InternalApplyStyle()

    {

      ShowInactiveSegments=_showinactivesegments;

    }

  }

}

 

Return to the article

Copyright © Bob Powell 2003-2009. All rights reserved