Quantcast
Channel: API Programming »» VB.NET « API Programming
Viewing all articles
Browse latest Browse all 8

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

$
0
0

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;
}

Viewing all articles
Browse latest Browse all 8

Trending Articles