Export (0) Print
Expand All

ToolStripProgressBar Class

Represents a Windows progress bar control contained in a StatusStrip.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public class ToolStripProgressBar : ToolStripControlHost

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.

NoteNote:

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.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;

class FibonacciNumber : Form
{
	[STAThread]
	static void Main()
	{
		Application.EnableVisualStyles();
		Application.Run(new FibonacciNumber());
	}

	private StatusStrip progressStatusStrip;
	private ToolStripProgressBar toolStripProgressBar;
	private NumericUpDown requestedCountControl;
	private Button goButton;
	private TextBox outputTextBox;
	private BackgroundWorker backgroundWorker;
	private ToolStripStatusLabel toolStripStatusLabel;
	private int requestedCount;

	public FibonacciNumber()
	{
		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);

		FlowLayoutPanel flp = new FlowLayoutPanel();
		flp.Dock = DockStyle.Top;

		Label beforeLabel = 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);
		Label afterLabel = new Label();
		afterLabel.Text = "Numbers in the Fibonacci sequence.";
		afterLabel.AutoSize = true;
		flp.Controls.Add(afterLabel);
		
		goButton = new Button();
		goButton.Text = "&Go";
		goButton.Click += new System.EventHandler(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;
		backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
		backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
		backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
		
	}

	private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
	{
		// 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...");
		Decimal lastlast = 0;
		Decimal last = 1;
		Decimal current;
		if (requestedCount >= 1)
		{ AppendNumber(0); }
		if (requestedCount >= 2)
		{ AppendNumber(1); }
		for (int i = 2; i < requestedCount; ++i)
		{
			// Calculate the number.
			checked { 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;
		}


		backgroundWorker.ReportProgress(100, "Complete!");
	}

	private delegate void AppendNumberDelegate(Decimal number);
	private void AppendNumber(Decimal number)
	{
		if (outputTextBox.InvokeRequired)
		{ outputTextBox.Invoke(new AppendNumberDelegate(AppendNumber), number); }
		else
		{ outputTextBox.AppendText(number.ToString("N0") + Environment.NewLine); }
	}
	private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
	{
		toolStripProgressBar.Value = e.ProgressPercentage;
		toolStripStatusLabel.Text = e.UserState as String;
	}

	private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
	{
		if (e.Error is OverflowException)
		{ outputTextBox.AppendText(Environment.NewLine + "**OVERFLOW ERROR, number is too large to be represented by the decimal data type**"); }
		toolStripProgressBar.Enabled = false;
		requestedCountControl.Enabled = true;
		goButton.Enabled = true;

	}

	private void button1_Click(object sender, EventArgs e)
	{
		goButton.Enabled = false;
		toolStripProgressBar.Enabled = true;
		requestedCount = (int)requestedCountControl.Value;
		requestedCountControl.Enabled = false;
		outputTextBox.Clear();
		backgroundWorker.RunWorkerAsync();
	}
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft