Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,046 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,530 people online right now. Registration is fast and FREE... Join Now!




Improved ProgressBar

 
Reply to this topicStart new topic

> Improved ProgressBar, Negative values, Larger range

AdamSpeight2008
Group Icon



post 4 Aug, 2008 - 06:18 AM
Post #1


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 control
Create 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 functionality
With 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 Bar
Add 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 colors
Nearly 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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

gbertoli3
Group Icon



post 10 Oct, 2008 - 09:17 PM
Post #2
For some reason it is not drawing the bars. I have the exact code you have only I converted it to C#.
Go to the top of the page
+Quote Post

AdamSpeight2008
Group Icon



post 10 Oct, 2008 - 10:57 PM
Post #3
QUOTE(gbertoli3 @ 11 Oct, 2008 - 06:17 AM) *

For some reason it is not drawing the bars. I have the exact code you have only I converted it to C#.

You may are converted it incorrectly.
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 11 Oct, 2008 - 08:24 AM
Post #4
No I did the conversion myself, then I used an online one and got the same result. I will try compiling it in VB.NET
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EDIT:

Well for some reason it works in VB.NET

I will double check my code.

Thanks for the help

This post has been edited by gbertoli3: 11 Oct, 2008 - 08:25 AM
Go to the top of the page
+Quote Post

RagedPWNLord
*



post 25 Oct, 2008 - 07:20 AM
Post #5
Thx, this is cool crazy.gif I have learned so much VB this week smile.gif i can make password protected forms now without tutorial smile.gif nd that's easy LOL

Some one need a simple password thing just ask me and i'll make a tutorial wink2.gif
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/1/08 05:05PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month