Home > C#, Uncategorized, VB.NET > How to Use the Progress Bar in VB.NET and C#

How to Use the Progress Bar in VB.NET and C#

The progress bar is one of those universal objects that everyone recognizes, it’s useful when doing a large or long task. For example, if your updating/deleting/copying a bunch of files you can update the progress bar as you modify each file.

Progress Bar App with VB.NET and C#

Progress Bar App with VB.NET and C#

There are several lines of code to pay attention to, let me outline them here in further detail.
Line 7 – we can think of the maximum as how many steps their are. We can say we want a 1000 steps, or only 20 steps. This makes it useful if you want to update a random number of objects stored in an array, in that case we could store the ProgressBar1.Maximum = UBound(arrSomeArray).
Line 9 – we are just initializing our progress bar to 0
Line 11 – we make the progress bar visible. This step is up to you, I infrequently use the progress bar and I usually keep it hidden until I have a reason to display it. If your progress bar is already visible you can delete this line of code
Line 13 – this line is completely unrelated to the progress bar, whatever value is specified in the Sleep argument is how long your application will wait to update. In this application, I decided to wait 1000 milliseconds (or 1 second) before I goto the next step, this will allow us to see the progress bar update so we can simulate a test scenario.
Line 15-29 – This is just updating the progress bar, then waiting some more, nothing to special going on here. As you can see the value I am raising the progress bar is completely arbitrary.
Line 30 – Print out a message box saying complete
Line 31 – Hide the progress bar so that it is no longer visible, we’ve already told the user that the task is complete, there’s no need to continue showing the progress bar. Again, this line of code is entirely up to you.

This is the code for a VB.NET progress bar example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'This code was developed by Gerardo Lopez and was downloaded from www.brangle.com
'Complete source code is available at:
'http://www.brangle.com/wordpress/2009/08/how-to-use-the-progress-bar-in-vb-net-and-c-sharp/
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Set the total number of steps to 100 (or 100%)
    ProgressBar1.Maximum = 100
    'Set the initial value of the progress bar to 0% completed
    ProgressBar1.Value = 0
    'If your progress bar is already visible you don't need this. But this is one of those objects I like to hide when I'm not using it
    ProgressBar1.Visible = True
    'This next line tells your application to wait or go to sleep for 1000ms / 1 second
    System.Threading.Thread.Sleep(1000)
    'Update the progress bar to 15% completed
    ProgressBar1.Value = 15
    'Wait for another second
    System.Threading.Thread.Sleep(1000)
    'Update to 30% complete
    ProgressBar1.Value = 30
    System.Threading.Thread.Sleep(1000)
    ProgressBar1.Value = 45
    System.Threading.Thread.Sleep(1000)
    ProgressBar1.Value = 60
    System.Threading.Thread.Sleep(1000)
    ProgressBar1.Value = 75
    System.Threading.Thread.Sleep(1000)
    ProgressBar1.Value = 90
    System.Threading.Thread.Sleep(1000)
    ProgressBar1.Value = 100
    MsgBox("Complete!")
    ProgressBar1.Visible = False
End Sub

This is the source code for a C# progress bar example
The code only varies slightly in C#, I’ll make sure to explain any differences. Besides the semicolons at the end of every line, only lines 12, 31, and 32 differ from the VB.NET example.
Line 12, Line 32 – Boolean values must be in lower case.
Line 31 – Showing a message box is slightly different in VB.NET, if you didn’t add the following headers using System.Windows.Forms; in your source code, then you will need to specify the full class name. I included it so that this example can work on as many peoples projects as possible. If you are already including Systems.Windows.Forms then you can show a message box by replacing line 31 with: MessageBox.Show(“Complete!”);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//This code was developed by Gerardo Lopez and was downloaded from www.brangle.com
//Complete source code is available at:
//http://www.brangle.com/wordpress/2009/08/how-to-use-the-progress-bar-in-vb-net-and-c-sharp/
 
private void button1_Click(object sender, EventArgs e)
{
    //Set the total number of steps to 100 (or 100%)
    progressBar1.Maximum = 100;
    //Set the initial value of the progress bar to 0% completed
    progressBar1.Value = 0;
    //If your progress bar is already visible you don't need this. But this is one of those objects I like to hide when I'm not using it
    progressBar1.Visible = true;
    //This next line tells your application to wait or go to sleep for 1000ms / 1 second
    System.Threading.Thread.Sleep(1000);
    //Update the progress bar to 15% completed
    progressBar1.Value = 15;
    //Wait for another second
    System.Threading.Thread.Sleep(1000);
    //Update to 30% complete
    progressBar1.Value = 30;
    System.Threading.Thread.Sleep(1000);
    progressBar1.Value = 45;
    System.Threading.Thread.Sleep(1000);
    progressBar1.Value = 60;
    System.Threading.Thread.Sleep(1000);
    progressBar1.Value = 75;
    System.Threading.Thread.Sleep(1000);
    progressBar1.Value = 90;
    System.Threading.Thread.Sleep(1000);
    progressBar1.Value = 100;
    System.Windows.Forms.MessageBox.Show("Complete!");
    progressBar1.Visible = false;
}
Categories: C#, Uncategorized, VB.NET Tags: , , ,
  1. Dhruv
    October 3rd, 2009 at 03:31 | #1

    Thanks for post !!!
    help me lot….. 🙂

  2. December 10th, 2009 at 14:04 | #2

    thanks for the share dear

  1. November 10th, 2019 at 14:40 | #1
  2. February 23rd, 2020 at 01:05 | #2
  3. February 29th, 2020 at 06:09 | #3
  4. April 8th, 2020 at 21:52 | #4
  5. April 15th, 2020 at 23:44 | #5
  6. May 15th, 2020 at 15:09 | #6
  7. June 4th, 2020 at 05:14 | #7
  8. September 12th, 2020 at 01:40 | #8
  9. September 23rd, 2020 at 11:51 | #9
  10. September 25th, 2020 at 09:29 | #10
  11. September 27th, 2020 at 04:53 | #11
  12. September 27th, 2020 at 05:00 | #12
  13. October 8th, 2020 at 11:42 | #13
  14. October 11th, 2020 at 21:12 | #14
  15. October 16th, 2020 at 08:33 | #15
  16. October 22nd, 2020 at 04:27 | #16
  17. November 3rd, 2020 at 16:08 | #17
  18. May 15th, 2021 at 16:30 | #18
  19. May 29th, 2021 at 06:25 | #19
  20. June 13th, 2021 at 14:32 | #20
  21. June 26th, 2021 at 21:47 | #21
  22. July 8th, 2021 at 13:01 | #22
  23. August 6th, 2021 at 14:26 | #23
  24. August 6th, 2021 at 17:26 | #24
  25. August 7th, 2021 at 08:56 | #25