Visual Basic .NET - ProgressBar Control

The ProgressBar control in VB.NET is used to display the progress of an operation. It shows the completion percentage of the task and helps to keep the user informed about the progress of the operation. In this tutorial, we will discuss the basic properties, methods, and events of the ProgressBar control with an example.

Creating a ProgressBar Control

  • To create a ProgressBar control in VB.NET, follow the below steps:
  • Open Visual Studio and create a new Windows Forms Application.
  • From the Toolbox, drag and drop a ProgressBar control onto the form.
  • The ProgressBar control will be added to the form.
  • Basic Properties of ProgressBar Control

The following are some of the basic properties of the ProgressBar control:

  • Minimum: The minimum value of the progress bar.
  • Maximum: The maximum value of the progress bar.
  • Value: The current value of the progress bar.
  • Step: The value to be added to the progress bar when PerformStep() method is called.
  • Style: The style of the progress bar. The available styles are Blocks, Continuous, Marquee, and Vertical.

Basic Methods of ProgressBar Control

  • The following are some of the basic methods of the ProgressBar control:
  • Increment(Int32): Increases the value of the progress bar by the specified amount.
  • PerformStep(): Increases the value of the progress bar by the value of the Step property.
  • Reset(): Resets the value of the progress bar to the value of the Minimum property.

Basic Events of ProgressBar Control

The following are some of the basic events of the ProgressBar control:

  • Click: Occurs when the user clicks the control.
  • ValueChanged: Occurs when the Value property of the control changes.

Example

Let's create an example to demonstrate the use of the ProgressBar control. We will create a simple program that will use the ProgressBar control to display the progress of a task.

  • Open Visual Studio and create a new Windows Forms Application.
  • From the Toolbox, drag and drop a ProgressBar control onto the form.
  • Drag and drop a Button control onto the form.
  • Double-click on the Button control to create a click event handler.
  • In the click event handler, add the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = 100
    ProgressBar1.Step = 1
    For i As Integer = 0 To 100
        ProgressBar1.Value = i
        System.Threading.Thread.Sleep(50)
    Next
End Sub

In the above code, we have set the Minimum, Maximum, and Step properties of the ProgressBar control. We have also added a For loop that will loop through the values from 0 to 100 and set the value of the ProgressBar control to the current loop value. We have added a System.Threading.Thread.Sleep(50) statement inside the loop to simulate some processing time.

Run the program and click on the Button control.

The ProgressBar control will display the progress of the task.

In this example, we have used a For loop to simulate a task and update the ProgressBar control with the progress of the task. You can use the ProgressBar control to display the progress of any operation that takes some time to complete, such as copying a file or downloading a file from the internet.