Home » Category » DotNet Visual Basic

DotNet Visual Basic: Yet another progress bar problem

104| Tue, 04 Dec 2007 01:17:00 GMT| kal_torak| Comments (4)
I am trying to make a timer with nice gradient that you set the number of seconds, then start it counting down, by the gradient filling the length of the bar.
But I can't figure out how to make it work.
Does anyone have a good theory for this, or better yet, code? :D

Keywords & Tags: progress, bar, dotnet, visual basic, vb, .net

URL: http://dotnet.itags.org/dotnet-visual-basic/171/
 
«« Prev - Next »» 4 helpful answers below.
If you're doing this as a learning experience then you'll want to learn how to do it yourself with GDI+. If you just want the effect then you might want to use the DoneOMeter control in the ElementsEx library (see my signature for link), which allows you to set an image to fill a progressbar.

jmcilhinney | Tue, 04 Dec 2007 14:31:00 GMT |

Here is an incredibly simple implementation.. one you can add timers to, and a better gradient fill using a graphics path.

Public Class GradientProgressBar
Inherits UserControl
Private _percent As Decimal = 0
Public Property Percent() As Integer
Get
Return Convert.ToInt32(_percent * 100)
End Get
Set(ByVal Value As Integer)
If (Value < 101) And (Value >= 0) Then
_percent = Convert.ToDecimal(Value / 100)
Me.Invalidate()
End If
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawRectangle(Pens.Black, 0, 0, MyBase.Size.Width - 1, MyBase.Size.Height - 1)
e.Graphics.FillRectangle(New Drawing2D.LinearGradientBrush(New Point(1, 1), New Point(MyBase.Size.Width - 2, MyBase.Size.Height - 2), Color.Blue, Color.Teal), 1, 1, Convert.ToInt32((MyBase.Size.Width - 2) * _percent), MyBase.Size.Height - 2)
End Sub
End Class

'How to use it.
Dim p As New GradientProgressBar
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
p.Location = New Point(20, 20)
p.Size = New Size(200, 25)
Me.Controls.Add(p) 'Critical.
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r As New Random
p.Percent = r.Next(0, 101)
End Sub

Bill

conipto | Tue, 04 Dec 2007 14:32:00 GMT |

Okies, I'll poke around with that.
My problem is not the drawing part, I have made a nice little progress bar before that showed the progress of an operation. The part I'm having trouble with is figuring out how to use a timer to dictate how long the progress bar runs.

Thanks both of ya :D

kal_torak | Tue, 04 Dec 2007 14:33:00 GMT |

So how do you know if the user is using a P90 with 32 megs of ram. Or using Norton Internet Security that slows their system to a crawl. Or has some spyware installed that makes the PC twice as slow.. etc..etc. You don't. All you know, from a programming perspective, is how complete your installation/event task is at various points. This is where you as a programmer make the thing make sense. For instance, if you were using my silly progress bar, and say at various points to change your taskbar. Simply having it work via a timer is an easy thing, though not very accurate considering the differences in people's programs.

For example, with my dumb little 5 minute control, to use a timer, you might go:

myGradientProgressBar.Percent = 100
Dim WithEvents T as new Timer

Private Sub Button1_click(blah) handles blah
T.Interval = 500
T.Start()
End Sub

Private Sub T_Tick (byval Sender as Object, ByVal e as EventArgs) Handles T.Tick
myGradientProgressBar.Percent -= 10
End Sub

So that every half a second, it counts down 10 percent. A better way, if used as an actual progress bar, is to at various points in your program change the percentage done value (which mine and nearly all progress bar controls you might find should have) and update the graphic. I.e, in stage one, you might complete what you estimate to be 10% of the work, so when it's complete you move to 10%, at stage 2, another 40% is completed, so you move the progress bar to 50%, etc, etc.. until your program finishes whatever it's doing.

Bill

conipto | Tue, 04 Dec 2007 14:34:00 GMT |

DotNet Visual Basic Hot Answers

DotNet Visual Basic New questions

DotNet Visual Basic Related Categories