ToolStripProgressBar Class
Represents a Windows progress bar control contained in a StatusStrip.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
ToolStripProgressBar is the ProgressBar optimized for hosting in a ToolStrip. A subset of the hosted control's properties and events are exposed at the ToolStripProgressBar level, but the underlying ProgressBar control is fully accessible through the ProgressBar property.
A ToolStripProgressBar control visually indicates the progress of a lengthy operation. The ToolStripProgressBar control displays a bar that fills in from left to right with the system highlight color as an operation progresses.
Note: |
|---|
The ToolStripProgressBar control can only be oriented horizontally. |
The ToolStripProgressBar control is typically used when an application performs tasks such as copying files or printing documents. Users of an application might consider an application unresponsive if there is no visual cue. Use the ToolStripProgressBar to notify the user that the application is performing a lengthy task and that the application is still responding.
The Maximum and Minimum properties define the range of values to represent the progress of a task. The Minimum property is typically set to a value of zero, and the Maximum property is typically set to a value indicating the completion of a task. For example, to display the progress properly when copying a group of files, the Maximum property could be set to the total number of files to be copied. The Value property represents the progress that the application has made toward completing the operation. Because the bar displayed in the control is a collection of blocks, the value displayed by the ToolStripProgressBar only approximates the Value property's current value. Based on the size of the ToolStripProgressBar, the Value property determines when to display the next block.
There are a number of ways to modify the value displayed by the ToolStripProgressBar other than changing the Value property directly. You can use the Step property to specify a specific value to increment the Value property by, and then call the PerformStep method to increment the value. To vary the increment value, you can use the Increment method and specify a value by which to increment the Value property.
ToolStripProgressBar replaces the older ProgressBar control, which is nevertheless retained for backward compatibility.
The following code example demonstrates a ToolStripProgressBar that calculates a sequence of Fibonacci numbers.
Imports System Imports System.Collections.Generic Imports System.Windows.Forms Imports System.ComponentModel Class FibonacciNumber Inherits Form <STAThread()> _ Shared Sub Main() Application.EnableVisualStyles() Application.Run(New FibonacciNumber()) End Sub Private progressStatusStrip As StatusStrip Private toolStripProgressBar As ToolStripProgressBar Private requestedCountControl As NumericUpDown Private goButton As Button Private outputTextBox As TextBox Private backgroundWorker As BackgroundWorker Private toolStripStatusLabel As ToolStripStatusLabel Private requestedCount As Integer Public Sub New() [Text] = "Fibonacci" ' Prepare the StatusStrip. progressStatusStrip = New StatusStrip() toolStripProgressBar = New ToolStripProgressBar() toolStripProgressBar.Enabled = False toolStripStatusLabel = New ToolStripStatusLabel() progressStatusStrip.Items.Add(toolStripProgressBar) progressStatusStrip.Items.Add(toolStripStatusLabel) Dim flp As New FlowLayoutPanel() flp.Dock = DockStyle.Top Dim beforeLabel As New Label() beforeLabel.Text = "Calculate the first " beforeLabel.AutoSize = True flp.Controls.Add(beforeLabel) requestedCountControl = New NumericUpDown() requestedCountControl.Maximum = 1000 requestedCountControl.Minimum = 1 requestedCountControl.Value = 100 flp.Controls.Add(requestedCountControl) Dim afterLabel As New Label() afterLabel.Text = "Numbers in the Fibonacci sequence." afterLabel.AutoSize = True flp.Controls.Add(afterLabel) goButton = New Button() goButton.Text = "&Go" AddHandler goButton.Click, AddressOf button1_Click flp.Controls.Add(goButton) outputTextBox = New TextBox() outputTextBox.Multiline = True outputTextBox.ReadOnly = True outputTextBox.ScrollBars = ScrollBars.Vertical outputTextBox.Dock = DockStyle.Fill Controls.Add(outputTextBox) Controls.Add(progressStatusStrip) Controls.Add(flp) backgroundWorker = New BackgroundWorker() backgroundWorker.WorkerReportsProgress = True AddHandler backgroundWorker.DoWork, AddressOf backgroundWorker1_DoWork AddHandler backgroundWorker.RunWorkerCompleted, AddressOf backgroundWorker1_RunWorkerCompleted AddHandler backgroundWorker.ProgressChanged, AddressOf backgroundWorker1_ProgressChanged End Sub Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) ' This method will run on a thread other than the UI thread. ' Be sure not to manipulate any Windows Forms controls created ' on the UI thread from this method. backgroundWorker.ReportProgress(0, "Working...") Dim lastlast As [Decimal] = 0 Dim last As [Decimal] = 1 Dim current As [Decimal] If requestedCount >= 1 Then AppendNumber(0) End If If requestedCount >= 2 Then AppendNumber(1) End If Dim i As Integer While i < requestedCount ' Calculate the number. current = lastlast + last ' Introduce some delay to simulate a more complicated calculation. System.Threading.Thread.Sleep(100) AppendNumber(current) backgroundWorker.ReportProgress(100 * i / requestedCount, "Working...") ' Get ready for the next iteration. lastlast = last last = current i += 1 End While backgroundWorker.ReportProgress(100, "Complete!") End Sub Delegate Sub AppendNumberDelegate(number As [Decimal]) Private Sub AppendNumber(number As [Decimal]) If outputTextBox.InvokeRequired Then outputTextBox.Invoke(New AppendNumberDelegate(AddressOf AppendNumber), number) Else outputTextBox.AppendText((number.ToString("N0") + Environment.NewLine)) End If End Sub Private Sub backgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) toolStripProgressBar.Value = e.ProgressPercentage toolStripStatusLabel.Text = e.UserState ' End Sub Private Sub backgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) If TypeOf e.Error Is OverflowException Then outputTextBox.AppendText((Environment.NewLine + "**OVERFLOW ERROR, number is too large to be represented by the decimal data type**")) End If toolStripProgressBar.Enabled = False requestedCountControl.Enabled = True goButton.Enabled = True End Sub Private Sub button1_Click(sender As Object, e As EventArgs) goButton.Enabled = False toolStripProgressBar.Enabled = True requestedCount = Fix(requestedCountControl.Value) requestedCountControl.Enabled = False outputTextBox.Clear() backgroundWorker.RunWorkerAsync() End Sub End Class
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.ToolStripItem
System.Windows.Forms.ToolStripControlHost
System.Windows.Forms.ToolStripProgressBar
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: