Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

(AGauge) WinForms Gauge Control

4.93/5 (27 votes)
29 Aug 2012Zlib1 min read 168.1K  
(AGauge) WinForms Gauge control

Introduction

Image 1

Original AGauge control

AGauge is a gauge control for WinForms create by A.J.Bauer using GDI+. The original code was published in "Code Project - A fast and performing gauge" The version that I published here is an improved version of AGauge which contains the following changes.

Improvements

Dynamic Gauge Label and Gauge Range

Properties for gauge label (previously known as CapText) and range are grouped into GaugeRanges and GaugeLabels which allow us to create any number of range and label as we wish. Range and label can be edited either from code or using the collection editor from the properties window.

C#
[System.ComponentModel.Browsable(true),
System.ComponentModel.Category("AGauge"),
System.ComponentModel.Description("Gauge Ranges.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public AGaugeRangeCollection GaugeRanges { get { return _GaugeRanges; } }
private AGaugeRangeCollection _GaugeRanges;

[System.ComponentModel.Browsable(true),
System.ComponentModel.Category("AGauge"),
System.ComponentModel.Description("Gauge Labels.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public AGaugeLabelCollection GaugeLabels { get { return _GaugeLabels; } }
private AGaugeLabelCollection _GaugeLabels;

Besides, each label can use different Font settings since label is an instance of AGaugeLabel.

Image 2

Added NeedleType Enumeration

AGauge control has 2 different types of needle design selectable from NeedleType property. Type of NeedleType property was changed from Int32 (0 or 1) to enumeration type (NeedleType.Advance or NeedleType.Simple) to avoid invalid entry from user.

Image 3

Events

Update ValueInRangeChangedEvent

ValueInRangeChangedDelegate was changed to ValueInRangeChangedEvent to allow multiple subscriptions. The event is changed to trigger only if value is entering or leaving a defined range. Besides, ValueInRangeChangedEventArgs was updated to hold current range and gauge value.

C#
[Description("This event is raised if the value is entering or leaving
  defined range.")]
public event EventHandler<ValueInRangeChangedEventArgs> ValueInRangeChanged;
private void OnValueInRangeChanged(AGaugeRange range, Single value)
{
  EventHandler<ValueInRangeChangedEventArgs> e = ValueInRangeChanged;
  if (e != null) e(this, new ValueInRangeChangedEventArgs(range, value,
                                                            range.InRange));
}

/// <summary>
/// Event argument for <see cref="ValueInRangeChanged"/> event.
/// </summary>
public class ValueInRangeChangedEventArgs : EventArgs
{
  /// <summary>
  /// Affected GaugeRange
  /// </summary>
  public AGaugeRange Range { get; private set; }
  /// <summary>
  /// Gauge Value
  /// </summary>
  public Single Value { get; private set; }
  /// <summary>
  /// True if value is within current range.
  /// </summary>
  public bool InRange { get; private set; }
  public ValueInRangeChangedEventArgs(AGaugeRange range, Single value,
                                          bool inRange)
  {
    this.Range = range;
    this.Value = value;
    this.InRange = inRange;
  }
}

Added ValueChangedEvent

ValueChanged event is added to notify user whenever gauge value is updated. Note that attempting to set gauge value out of defined gauge range will not trigger this event.

C#
[Description("This event is raised when gauge value changed.")]
public event EventHandler ValueChanged;
private void OnValueChanged()
{
  EventHandler e = ValueChanged;
  if (e != null) e(this, null);
}

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License

History

  • 28/08/2012: Initial release (Version 2.0.0)

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License