Muhnamana
Muhnamana

Reputation: 1044

Updating a Progress Bar within the Status Bar

I'm trying to figure out how to incorporate a progress bar within the status bar to show how much processing is completed. Below is my example of updating the progress bar (not sure if this is the correct way or not)

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 2
    If ToolStripProgressBar1.Value = 100 Then
        ToolStripProgressBar1.Value = 0
        ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 2
        Timer1.Enabled = True
    End If
End Sub

Here is the code within the button.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1Run.Click
    ToolStripStatusLabel1.Text = "Processing..."
    Timer1.Enabled = True

    'more code to be inserted here        

End Sub

What I'm not sure is how to update the progress bar based on the amount of code you have and once the processing is complete, update the ToolStripStatusLabel1 to show "Processing...Complete!".

Upvotes: 1

Views: 12692

Answers (2)

LarsTech
LarsTech

Reputation: 81620

Usually, you would use the PerformStep() method to update the ProgressBar. This action will increment the value of the ProgressBar by the value entered in the Step property.

The ProgessBar also has a Maximum property to determine when the progress is at 100%. That is, if the Maximum=100 and the Value=100, the ProgressBar should show full.

Typical setup:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1Run.Click
  ToolStripProgressBar1.Maximum = 100
  ToolStripProgressBar1.Step = 2
  ToolStripProgressBar1.Value = ToolStripProgressBar1.Minimum
  ToolStripStatusLabel1.Text = "Processing..."
  Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
  ToolStripProgressBar1.PerformStep()
  If ToolStripProgressBar1.Value >= ToolStripProgressBar1.Maximum Then
    Timer1.Stop()
    ToolStripStatusLabel1.Text = "Completed"
    ToolStripProgressBar1.Value = ToolStripProgressBar1.Minimum
  End If
End Sub

A timer is usually an odd type of measurement for showing code progression. A ProgressBar usually is used with a BackgroundWorker to show the progress of your code.

Upvotes: 1

JeffO
JeffO

Reputation: 8043

If ToolStripProgressBar1.Value = 100 Then

Are you sure your value will ever get to "exactly" 100? Maybe

If ToolStripProgressBar1.Value >= 100 Then

Or is 100 some magic number?

EDIT: It really depends on the code you intend on running. The progress bar is just a visual indicator for the user that you have to set based on a particular process. If you just want it to display to show something is happenning (similar to an hour glass icon) this should work.

Somewhere you need to Set the timer.enabled to false and cleanup the progress bar.

Upvotes: 0

Related Questions