This tutorial will demonstrate how to use and how progress bar in vb.net works.
What is a progress bar?
A progress bar is a graphical control element used to visualize the progression of an extended computer operation, such as a download, file transfer, or installation.
Let’s start the tutorial
- Open your Microsoft Visual Studio 2010
- On the File menu select New Project
- Select Visual Basic then Windows Form Application. Click OK.
- We need to add the following controls to our form.
- 2 labels
- 1 timer
- And of course the progress bar control
- See the layout of the form below. (you are free to design and layout your form)
Note: Name the Label1 as lblload and Label2 as lblpercent.
- Double click Timer1 and paste the code.
ProgressBar1.Value = ProgressBar1.Value + 1
lblpercent.Text = ProgressBar1.Value / ProgressBar1.Maximum * 100 & " %"
If ProgressBar1.Value > 1 And ProgressBar1.Value < 30 Then
lblload.Text = "Loading program..."
Else
If ProgressBar1.Value > 30 And ProgressBar1.Value < 60 Then
lblload.Text = "Loading components..."
Else
If ProgressBar1.Value > 60 And ProgressBar1.Value < 100 Then
lblload.Text = "Loading database...please wait"
End If
End If
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop()
MessageBox.Show("Welcome")
Me.Close()
Exit Sub
End If
End If
Code Explanation:
The code above will increment the value of our progress bar by 1. As the progress bar increases, Label2 or the lblpercent will display its progress in percent. Label1 or the lblload will display a text of “Loading program…” if the value of the progress bar is ranging from 1 to 30, “Loading components…” if progress bar is 30 to 60 and “Loading database… please wait” if value is 60 up to 100. The progress bar will stop if it reaches 100 and a message box will appear displaying “Welcome”.
Note: make sure the code is placed between the lines of:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
- Lastly on the Load event of the form (just double the form) place the code.
Timer1.Start()
It will automatically start the loading of progress bar.
- Save and Run the project.
Happy Coding! More tutorials to come (C#, Java, C++ and many more)