Link to home
Start Free TrialLog in
Avatar of Jason Paradis
Jason ParadisFlag for United States of America

asked on

Configuring a ProgressBar to increase depending on length taken after buttonclick

I have added a ProgressBar and Timer to my project but it's behaving strangely. When I press a button it just increments the progress bar by 1 until it finishes regardless of how long the buttonclick event took. I would like to have it increment and fill to 100 when the event finishes.

Here is my code for the bar under my buttonclick event:

Private Sub FixAllButton_Click(ByVal sender As System.Object, e As EventArgs) Handles FixAllButton.Click
        MasterProgressBar.Value = 0
        MasterTimer.Start()
        FixAllButton.Enabled = False

Open in new window


Here is the rest of the code in the timer sub:

Private Sub MasterTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MasterTimer.Tick
        MasterProgressBar.Value += 1
        If MasterProgressBar.Value = 100 Then
            MasterTimer.Stop()
            FixAllButton.Enabled = True
        End If
    End Sub

Open in new window


How do I make the progress bar dynamically increase depending on the length of time it takes to complete the event and then fill when it ends?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

You need to know how long your process is taking and you need to increment your progress bar accordingly. This is easier said then done!

If you are in a loop, you know how many items you have to process and it is therefore easy to show a relatively accurate progress bar.

It is not always that simple!
We can be more helpful if we know what's in the rest of the button click event.
ASKER CERTIFIED SOLUTION
Avatar of Jason Paradis
Jason Paradis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I guess that would work not to let the ProgressBar to reach 100% in a standard Windows application but if your users ever say that the long running task that the button click event is processing ties up the UI and you try putting that code in a different thread so that the user is free to use the UI you will find out that it really does not work. It also hides the problem by stopping the progress bar before reaching 100.
Avatar of Jason Paradis

ASKER

No one provided a solution, just questions. I figured it out myself. It didn't need to be overly complicated. It works perfectly with what I wrote.