Complete Progress Bar

BleepyEvans

Active member
Joined
May 2, 2011
Messages
41
Programming Experience
1-3
Is there anyway to complete a progress bar just by going ProgressBar1.Value = 100 when you want it to finish.

I have a button which starts a countdown, I need the progress bar to complete when countdown reaches 0. Ive already used the step version, but when I want to alert the length of the countdown, the progress bar finishes too late or too early.

Thanks in advance
 
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Start()
        ProgressBar1.Value = 0
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 100
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Value += 1
        ProgressBar1.Refresh()
    End Sub

Try this out it worked for me. I think .Refresh should work and if not try something like Application.DoEvents which waits for the windows controls to catch up to the code.
 
Sorry that wont work.

Heres a section from my code, it counts down 15 seconds
VB.NET:
Public Class Form2

    Dim CountDownTime As DateTime

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CountDownTime = DateTime.Now.AddSeconds(15)
        countdowntimer.interval = 1
        countdowntimer.Start()
        wait15.interval = 15000
        wait15.Start()
    End Sub

    Private Sub CountDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles countdowntimer.Tick
        If CountDownTime < DateTime.Now Then
            CountDownTimeLabel.Text = "0:0.000"
            countdowntimer.Stop()
        Else
            Dim span As TimeSpan = CountDownTime.Subtract(DateTime.Now)
            CountDownTimeLabel.Text = span.Minutes.ToString & ":" & span.Seconds.ToString & "." & span.Milliseconds
        End If
    End Sub

    Private Sub wait15_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wait15.Tick
        start()
        wait15.Stop()
    End Sub
End Class

Any idea how I could implement a progress bar into that?
Ive added the timer intervals into the code just so u can get a feel for it, but I set them in the timer properties.
 
Well it will work, but if I put the ProgressBar1.Value += 1 in the CountDownTimer_Tick, then yes it will increase by one every 0.001 second (1 ms)

But then I have to work out what to set my maximum to ensure that the progressbar finishes when the countdown timer finishes.

-
15 seconds = 15000 ms

So if I set the progressbar maximum to 15000 then, in theory the progress bar should finish exactly when the 15 seconds are over.

But it doesnt happen, a maximum of 15000 is far too high, its infact somewhere between 800 and 1000.

-

I probably could find it out, but as im creating a customizable countdown timer, I dont think this would be the best way as I personally am not working out each value the progress bar maximum has to be for each countdown time.

-

Any other ideas?
 
You have start time and end time, so you have the total time. You also have elapsed time, so you can calculate progress percentage from that.
VB.NET:
Dim total = (endTime - startTime).TotalSeconds
Dim elapsed = (Date.Now - startTime).TotalSeconds
Dim progress = CInt(elapsed * 100 / total)
when progress is 100 or more the operation is complete and it is common to reset progress bar to 0 at this point.
 
Im not trying to find out the percentage of progress. Im trying to get the progress bar to finish exactly when my countdown does.
 
I think you may need to use Refresh and find a better interval to increment your progress bar because expecting the progress bar to update 15000 times in 15 seconds may cause some lag. But using max,min, and value you should be able to accomplish this.
 
I have written a program that does something similar. I have modified your code to do what you are trying to do. Hope this helps.

Dim CountDownTime As DateTime
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 100
CountDownTime = DateTime.Now.AddSeconds(15)
CountDownTimer.Interval = 1
CountDownTimer.Start()
wait15.Interval = 15000
wait15.Start()
End Sub
Private Sub CountDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles countdowntimer.Tick
If CountDownTime < DateTime.Now Then
CountDownTimeLabel.Text = "0:0.000"
countdowntimer.Stop()
Else
Dim span As TimeSpan = CountDownTime.Subtract(DateTime.Now)
CountDownTimeLabel.Text = span.Minutes.ToString &
":" & span.Seconds.ToString & "." & span.Milliseconds
ProgressBar1.Value = 100 - Int((span.TotalMilliseconds / 15000) * 100)
End If
End Sub
Private Sub wait15_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wait15.tick
CountDownTimer.Start()
wait15.Stop()
End Sub
 
This helps alot!, great 3rd Post. Althought dont forget code tags :p
Thank you very much

VB.NET:
Dim CountDownTime As DateTime

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 100
        CountDownTime = DateTime.Now.AddSeconds(15)
        CountDownTimer.Interval = 1
        CountDownTimer.Start()
        wait15.Interval = 15000
        wait15.Start()
    End Sub
    Private Sub CountDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles countdowntimer.Tick
        If CountDownTime < DateTime.Now Then
            CountDownTimeLabel.Text = "0:0.000"
            countdowntimer.Stop()
        Else
            Dim span As TimeSpan = CountDownTime.Subtract(DateTime.Now)
            CountDownTimeLabel.Text = span.Minutes.ToString & ":" & span.Seconds.ToString & "." & span.Milliseconds
            ProgressBar1.Value = 100 - Int((span.TotalMilliseconds / 15000) * 100)
        End If
    End Sub
    Private Sub wait15_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wait15.Tick
        CountDownTimer.Start()
        wait15.Stop()
    End Sub
 
Im not trying to find out the percentage of progress. Im trying to get the progress bar to finish exactly when my countdown does.
Does progressbar change because something is happening, or does something happen because progressbar changes? ;) I think you will find that progressbar is an indication of something happening in all cases if you think about it. If you can measure what is happening then you can indicate the progress, and the progressbar will always show correct indication of the operation.
 
No, was just trying to get the progress bar to finish exactly when a a countdown I made reach 0:0.000. Using timers wouldnt be the solution because the coutdown timer could be changed.

Anyway, its bin solved now :)
 
Back
Top