ProgressBar Control in C# and VB.NET

watch_later Sunday, April 14, 2019

Introduction


This article gives an explanation about how to use ProgressBar control in windows application using c#.net and vb.net. A ProgressBar control is used to represent the progress of a long operation that takes time wherever a user must look forward to the operation to be finished. It allows track and shows the progress of an operation.

ProgressBar Control in C# and VB.NET

So in this article, I'll demonstrate how to use a ProgressBar control in windows application using c#.net and vb.net and also discuss the different styles of ProgressBar, its properties and methods of the ProgressBar control as well as also show you how to use ProgressBar control dynamically step by step.

Requirement


1) Explain ProgressBar control with an example using c# and vb.net.
2) Explain how to add ProgressBar control dynamically.
3) Discuss the important properties and methods of the ProgressBar.

Implementation


So, as per given requirement let's create a simple demonstration of ProgressBar control using the c# and vb.net.

ProgressBar control


First, you have to open your visual studio and Select New >> Projects >> Select Windows Form Application and Give Name of your project. 

Now you can see one simple form has appeared on your screen with name Form1, So if you want to change your form name then you can change it from property window of your form.

Now, you have to open the toolbox and in that select ProgressBar control and drag and drop it on your form and same drag and drop two other simple buttons one for show ProgressBar and another for add ProgressBar control dynamically in the form and finally, your form looks like as shown below.

Add New ProgressBar Control

Now, You have to write a c# and vb.net code on click on "Load ProgressBar" button to fill and show the progress of your operation, here to demonstrate I took a fixed size integer number 105 as count and using for loop I have assigned value to the progress bar as you can see in the code.

C#

private void btnProgressbar_Click(object sender, EventArgs e)
{
    try
    {
        int Count = 105;
        for (int i = 0; i <= Count; i++)
        {
            progCodingvila.Value = Convert.ToInt32(Math.Floor(Convert.ToDouble((i * 100) / Count)));
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    
}

VB.NET

Private Sub btnProgressbar_Click(ByVal sender As ObjectByVal e As EventArgs)
        Try
            Dim Count As Integer = 105
            Dim i As Integer = 0
            Do While (i <= Count)
                progCodingvila.Value = Convert.ToInt32(Math.Floor(Convert.ToDouble(((i * 100) _
                                        / Count))))
                i = (i + 1)
            Loop
 
        Catch ex As Exception
            Throw ex
        End Try
 
    End Sub

Result

Load ProgressBar Control

Now, we will add the same ProgressBar control dynamically in our windows form on click event of "Add ProgressBar Dynamically" button and for that, you have to write a code for c# and vb.net as I showed below.

C#

private void btnProgressBarDynamic_Click(object sender, EventArgs e)
       {
           try
           {
               ProgressBar objprogBar = new ProgressBar();
               objprogBar.Location = new System.Drawing.Point(25, 135);
               objprogBar.Name = "progCodingvila1";
               objprogBar.Width = 350;
               objprogBar.Height = 17;
               objprogBar.Minimum = 0;
               objprogBar.Maximum = 100;
               objprogBar.Value = 85;
               Controls.Add(objprogBar);
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

VB.NET

Private Sub btnProgressBarDynamic_Click(ByVal sender As ObjectByVal e As EventArgs)
        Try
            Dim objprogBar As ProgressBar = New ProgressBar()
            objprogBar.Location = New System.Drawing.Point(25, 135)
            objprogBar.Name = "progCodingvila1"
            objprogBar.Width = 350
            objprogBar.Height = 17
            objprogBar.Minimum = 0
            objprogBar.Maximum = 100
            objprogBar.Value = 85
            Controls.Add(objprogBar)
        Catch ex As Exception
            Throw ex
        End Try
 
    End Sub

Result

Add ProgressBar Control Dynamically

Explanation


As you can see in the code I just create the object of the ProgressBar control  and then set some of the properties for that control such as Location where I assign X and Y position of the screen where ProgressBar control will appear dynamically, then also set property for height and width of ProgressBar control as well as also set value for it's Minimum, Maximum and value property and finally add this specified control to the control collection using Controls.Add method.

Properties and Methods of ProgressBar Control


1) Name: The Name property represents a unique name of a ProgressBar control.

2) Minimum: Minimum property describes a range of values.

3) Maximum: Maximum property describes a range of values.

4) Value: The Value property describes the current value of a ProgressBar

5) Dock: The Name property represents a position of a ProgressBar and it is used to set the position of a ProgressBar such as top, bottom, left, right and etc.

6) Height: The Name property represents a height of ProgressBar control.

7) Width: The Name property represents a width of ProgressBar control.

8) Styles of ProgressBar Control

Generally, ProgressBar Control has three different types of styles are available that you also can change from the property window of the ProgressBar control as I have shown below.


1) Blocks: The blocks property describes the progress by increasing the number of segmented blocks in a ProgressBar.

2) Continuous: The continuous property describes the progress by increasing the size of a smooth and continuous bar in a ProgressBar.

3) Marquee: The Marquee Property describes the progress by continuously scrolling a segmented block across a ProgressBar in a fashion like a marquee. 

Summary


In this article, we learned how to create ProgressBar control using c# and vb.net. First, we discussed what is ProgressBar Control and use of the ProgressBar. After that, we saw, how to use ProgressBar Control with a simple demonstration where we discussed how to use ProgressBar dynamically and finally, we discussed some of the important properties and methods of the ProgressBar Control.

Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.



sentiment_satisfied Emoticon