This documentation is archived and is not being maintained.

ProgressBar.Increment Method

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

[Visual Basic]
Public Sub Increment( _
   ByVal value As Integer _
)
[C#]
public void Increment(
 int value
);
[C++]
public: void Increment(
 int value
);
[JScript]
public function Increment(
   value : int
);

Parameters

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

Remarks

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.

Example

[Visual Basic, C#, C++] The following 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 object to provide a textual representation of the ProgressBar. This example assumes that you have a ProgressBar control, named progressBar1, and a StatusBar control that contains a StatusBarPanel, named statusBarPanel1. The Timer object, named time, must be added to the form as a member.

[Visual Basic] 
Private time As New Timer()

' Call this method from the constructor of the form.
Private Sub InitializeMyTimer()
   ' Set the interval for the timer.
   time.Interval = 250
   ' Connect the Tick event of the timer to its event handler.
   AddHandler time.Tick, AddressOf IncreaseProgressBar
   ' Start the timer.
   time.Start()
End Sub


Private Sub IncreaseProgressBar(ByVal sender As Object, ByVal e As EventArgs)
   ' 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 Then
      ' Stop the timer.
      time.Stop()
   End If
End Sub

[C#] 
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();
}

[C++] 
private:
   Timer* time;

   // Call this method from the constructor of the form.
   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(this, &Form1::IncreaseProgressBar);
      // Start the timer.
      time->Start();
   }

   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 = String::Concat( __box(progressBar1->Value), S"% 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();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ProgressBar Class | ProgressBar Members | System.Windows.Forms Namespace | Maximum | Minimum | Value | Step | PerformStep

Show: