Export (0) Print
Expand All

ProgressBar.Increment Method

Advances the current position of the progress bar by the specified amount.

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

public void Increment(
	int value
)

Parameters

value
Type: System.Int32

The amount by which to increment the progress bar's current position.

ExceptionCondition
InvalidOperationException

The Style property is set to Marquee

The Increment method enables you to increment the value of the progress bar by a specific amount. This method of incrementing the progress bar is similar to using the Step property with the PerformStep method. The Value property specifies the current position of the ProgressBar. If, after calling the Increment method, the Value property is greater than the value of the Maximum property, the Value property remains at the value of the Maximum property. If, after calling the Increment method with a negative value specified in the value parameter, the Value property is less than the value of the Minimum property, the Value property remains at the value of the Minimum property.

Because a ProgressBar object whose style is set to Marquee displays a continuously scrolling bar instead of its Value, calling Increment is unnecessary and will raise an InvalidOperationException.

The following code example demonstrates how to use the Increment method and the Value property to increment the value of a ProgressBar in the Tick event of a Timer. The example also displays the Value property in a StatusBarPanel to provide a textual representation of the ProgressBar. This example requires that you have a ProgressBar control, named progressBar1, and a StatusBar control that contains a StatusBarPanel, named statusBarPanel1. The Timer, named time, must be added to the form as a member.

private Timer time = new Timer();

// Call this method from the constructor of the form. 
private void InitializeMyTimer()
{
   // Set the interval for the timer.
   time.Interval = 250;
   // Connect the Tick event of the timer to its event handler.
   time.Tick += new EventHandler(IncreaseProgressBar);
   // Start the timer.
   time.Start();
}

private void IncreaseProgressBar(object sender, EventArgs e)
{
   // Increment the value of the ProgressBar a value of one each time.
   progressBar1.Increment(1);
   // Display the textual value of the ProgressBar in the StatusBar control's first panel.
   statusBarPanel1.Text = progressBar1.Value.ToString() + "% Completed";
   // Determine if we have completed by comparing the value of the Value property to the Maximum value. 
   if (progressBar1.Value == progressBar1.Maximum)
      // Stop the timer.
      time.Stop();
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft