Back to Devexpress

How To: Create a Linear Gauge (Runtime)

windowsforms-113855-controls-and-libraries-gauges-examples-how-to-create-a-linear-gauge-runtime.md

latest4.0 KB
Original Source

How To: Create a Linear Gauge (Runtime)

  • Nov 13, 2018
  • 2 minutes to read

The following example shows how to create a linear gauge.

In the example a linear gauge is created using the DevExpress.XtraGauges.Win.GaugeControlBase.AddLinearGauge method. The default elements (a scale, background layer and level bar) are added to the linear gauge via the LinearGauge.AddDefaultElements method. Then, some of them are customized in a specific manner.

The result is displayed below:

csharp
using DevExpress.XtraGauges.Win;
using DevExpress.XtraGauges.Win.Gauges.Linear;
using DevExpress.XtraGauges.Core.Model;
using DevExpress.XtraGauges.Core.Base;

GaugeControl gc = new GaugeControl();
// Add a linear gauge.
LinearGauge linearGauge = gc.AddLinearGauge();
// Add the default elements (a scale, background layer and level bar).
linearGauge.AddDefaultElements();
// Change the background layer's paint style.
LinearScaleBackgroundLayer background = linearGauge.BackgroundLayers[0];
background.ShapeType = BackgroundLayerShapeType.Linear_Style3;
// Customize the scale's settings.
LinearScaleComponent scale = linearGauge.Scales[0];
scale.MinValue = 0;
scale.MaxValue = 100;
scale.Value = 20;
scale.MajorTickCount = 6;
scale.MajorTickmark.FormatString = "{0:F0}";
scale.MajorTickmark.ShapeType = TickmarkShapeType.Linear_Style6_3;
scale.MajorTickmark.TextOffset = -20;
scale.MinorTickCount = 3;
scale.MinorTickmark.ShapeType = TickmarkShapeType.Linear_Style5_2;  
scale.AppearanceTickmarkText.TextBrush = new SolidBrushObject(Color.White);        
// Shift tick marks to the right.
scale.MajorTickmark.ShapeOffset = 5f;
scale.MinorTickmark.ShapeOffset = 5f;
// Change the levelBar's paint style.
LinearScaleLevelComponent levelBar = linearGauge.Levels[0];
levelBar.ShapeType = LevelShapeSetType.Style3;
// Shift the background layer up and to the left.
background.ScaleStartPos = new PointF2D(background.ScaleStartPos.X - 0.005f,
    background.ScaleStartPos.Y - 0.015f);
background.ScaleEndPos = new PointF2D(background.ScaleEndPos.X - 0.005f, 
    background.ScaleEndPos.Y);            
// Add the gauge control to the form.
gc.Size = new Size(250, 250);
gc.Parent = this;
vb
Imports DevExpress.XtraGauges.Win
Imports DevExpress.XtraGauges.Win.Gauges.Linear
Imports DevExpress.XtraGauges.Core.Model
Imports DevExpress.XtraGauges.Core.Base

Private gc As GaugeControl = New GaugeControl()
' Add a linear gauge.
Private linearGauge As LinearGauge = gc.AddLinearGauge()
' Add the default elements (a scale, background layer and level bar).
linearGauge.AddDefaultElements()
' Change the background layer's paint style.
Dim background As LinearScaleBackgroundLayer = linearGauge.BackgroundLayers(0)
background.ShapeType = BackgroundLayerShapeType.Linear_Style3
' Customize the scale's settings.
Dim scale As LinearScaleComponent = linearGauge.Scales(0)
scale.MinValue = 0
scale.MaxValue = 100
scale.Value = 20
scale.MajorTickCount = 6
scale.MajorTickmark.FormatString = "{0:F0}"
scale.MajorTickmark.ShapeType = TickmarkShapeType.Linear_Style6_3
scale.MajorTickmark.TextOffset = -20
scale.MinorTickCount = 3
scale.MinorTickmark.ShapeType = TickmarkShapeType.Linear_Style5_2
scale.AppearanceTickmarkText.TextBrush = New SolidBrushObject(Color.White)
' Shift tick marks to the right.
scale.MajorTickmark.ShapeOffset = 5f
scale.MinorTickmark.ShapeOffset = 5f
' Change the levelBar's paint style.
Dim levelBar As LinearScaleLevelComponent = linearGauge.Levels(0)
levelBar.ShapeType = LevelShapeSetType.Style3
' Shift the background layer up and to the left.
background.ScaleStartPos = New PointF2D(background.ScaleStartPos.X - 0.005f, _
    background.ScaleStartPos.Y - 0.015f)
background.ScaleEndPos = New PointF2D(background.ScaleEndPos.X - 0.005f, _
    background.ScaleEndPos.Y)
' Add the gauge control to the form.
gc.Size = New Size(250, 250)
gc.Parent = Me