I decided to create this control to over come some of the limitation of the standard progress bar.
- Allow negative values.
- Allow larger values.
Building the controlCreate a new User Control called UC_PBar.
First all of the properties and variable definitions.
vb
' Define a variable to store the current percentage of the bar, to save use recalculating
Private mPercentage As Single = 0.0
' Define a variable to store the Range, ABS(Min) + ABS(Max)
Private mRange As Long = 100
#Region "Properties"
#Region "Value Properties"
Private mMaximum As Long = 100
< _
System.ComponentModel.Category("Bar Settings") _
, System.ComponentModel.DisplayName("Maximum") _
, System.ComponentModel.Description("Maximum Value that the bar value can be.") _
> Public Property Maximum() As Long
Get
Return mMaximum
End Get
Set(ByVal value As Long)
If value = mMinimum Then
Throw New Exception("Maximums's value can not equal minimum.")
Exit Property
End If
If value = mMinimum Then
Throw New Exception("Maximums's value can be less than minimum.")
Exit Property
End If
If value < mValue Then
Throw New Exception("Maximums's value can not be less than value.")
Exit Property
End If
mMaximum = value
UpdateValues()
Me.Refresh()
End Set
End Property
Private mMinimum As Long = 0
< _
System.ComponentModel.Category("Bar Settings") _
, System.ComponentModel.DisplayName("Minimum") _
, System.ComponentModel.Description("Minimum Value that the bar value can be.") _
> Public Property Minimum() As Long
Get
Return mMinimum
End Get
Set(ByVal value As Long)
If value > mMaximum Then
Throw New Exception("Minimum can not exceed maximum.")
Exit Property
End If
If value = mMaximum Then
Throw New Exception("Minimum's value can not equal maximum.")
Exit Property
End If
If value > mValue Then
Throw New Exception("Minimum's value can not exceed value.")
Exit Property
End If
mMinimum = value
UpdateValues()
Me.Refresh()
End Set
End Property
Private mValue As Long = 0
< _
System.ComponentModel.Category("Bar Settings") _
, System.ComponentModel.DisplayName("Value") _
, System.ComponentModel.Description("Current value of the bar.") _
> Public Property Value() As Long
Get
Return mValue
End Get
Set(ByVal value As Long)
If value < mMinimum Then
Throw New Exception("Value is less than minimum.")
Exit Property
End If
If value > mMaximum Then
Throw New Exception("Value exceeds maximum allowed.")
Exit Property
End If
mValue = value
UpdateValues()
Me.Refresh()
End Set
End Property
#End Region
#Region "Color Properties"
Private mBGColor As Color
< _
System.ComponentModel.Category("Bar Settings") _
, System.ComponentModel.DisplayName("Background Color") _
, System.ComponentModel.Description("Color used as the background to the bar") _
> Public Property BGColor() As Color
Get
Return mBGColor
End Get
Set(ByVal value As Color)
mBGColor = value
Me.Refresh()
End Set
End Property
Private mBorderColor As Drawing.Color
< _
System.ComponentModel.Category("Bar Settings") _
, System.ComponentModel.DisplayName("Border Color") _
, System.ComponentModel.Description("Color ofthe border.") _
> Public Property BorderColor() As Drawing.Color
Get
Return mBorderColor
End Get
Set(ByVal value As Drawing.Color)
mBorderColor = value
Me.Refresh()
End Set
End Property
Private mBarColor As Drawing.Color
< _
System.ComponentModel.Category("Bar Settings") _
, System.ComponentModel.DisplayName("Bar Color") _
, System.ComponentModel.Description("Color of the percentage portion of the bar.") _
> Public Property BarColor() As Drawing.Color
Get
Return mBarColor
End Get
Set(ByVal value As Drawing.Color)
mBarColor = value
Me.Refresh()
End Set
End Property
#End Region
#End Region
Adding the functionalityWith all the properties out the way, we need to now some code which uses the updated values
vb
Private Sub UpdateValues()
' Caluculate the value for range
mRange = Math.Abs(mMinimum-mMaximum) 'Minor error in calculating range. doh!
' Define where the zero point is.
Dim ZeroPoint As Long
ZeroPoint = Math.Abs(mMinimum)
' Work out where value lays in the range
Dim tValue As Long
tValue = ZeroPoint + mValue
' Caluculate this as percentage of the range.
mPercentage = tValue / mRange
End Sub
Drawing the Progress BarAdd the code to handle the drawing of the Progress bar.
vb
Private Sub UControl_PBar_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
DrawPBar(e.Graphics)
End Sub
Private Sub DrawPBar(ByRef g As Graphics)
' Get out pens
Dim mBGPen As New Pen(mBGColor, 1)
Dim mBordPen As New Pen(mBorderColor, 2)
Dim mBarPen As New Pen(mBarColor, 1)
' Draw Bar using pens
g.FillRectangle(mBGPen.Brush, 0, 0, Me.Width, Me.Height)
g.FillRectangle(mBarPen.Brush, 0, 0, CSng(Me.Width * mPercentage), Me.Height)
g.DrawRectangle(mBordPen, 0, 0, Me.Width, Me.Height)
' Tidy up Pens
mBarPen.Dispose()
mBordPen.Dispose()
mBGPen.Dispose()
End Sub
Setting up default colorsNearly there just to little bit of code to set the default color for the bar.
vb
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Set the Default color values
mBGColor = Drawing.SystemColors.Control
mBorderColor = Color.Black
mBarColor = Color.Red
End Sub
Build the project, now you have access to the control in the toolbox.
This post has been edited by AdamSpeight2008: 7 Aug, 2008 - 04:52 PM