A step by step example to help you learn ProgressBar, which is a
System.Windows.Forms
component for rendering progress state of an operation.
This tutorial will help you learn the following concepts:
Example 1: Create ProgressBars and Update them
This example will demonstrate how create multiple progressbars programmatically and update them via
Timer
component.
This example will comprise the following files:
ProgressBar.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project
Step 2: Write Code
Write Code as follows:
*(a). ProgressBar.cs
Create a file named ProgressBar.cs
Start by adding the following imports. The System.Windows.Forms
will host our ProgressBar
.
using System;
using System.Windows.Forms;
Create our Form
class Form1 : Form {
Define our main()
method, The main entry point for the application.
```csharp
public static void Main() {</code></pre>
<p>Enable Visual styles and run the application:</p>
<pre><code class="language-csharp"> Application.EnableVisualStyles();
Application.Run(new Form1());</code></pre>
<p>Define our constructor and set the <code>text</code> property of the form</p>
<pre><code class="language-csharp"> public Form1() {
Text = "ProgressBar Example";
Set properties of the ProgressBar like parent, location, style, width etc
```csharp
progressBar1.Parent = this;
progressBar1.Location = new System.Drawing.Point(50, 50);
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Width = 200;
//...
Set timer interval
timer.Interval = 50;
timer.Enabled = true;
Handle timer tick event and update progressBar value
timer.Tick += delegate (object sender, EventArgs e) {
progressBar4.Value = progressBar4.Value < progressBar4.Maximum ? progressBar4.Value + 1 : progressBar4.Minimum;
};
Define progressBars and Timer as instance fields
private ProgressBar progressBar1 = new ProgressBar();
private ProgressBar progressBar2 = new ProgressBar();
private ProgressBar progressBar3 = new ProgressBar();
private ProgressBar progressBar4 = new ProgressBar();
private ProgressBar progressBar5 = new ProgressBar();
private Timer timer = new Timer();
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
Text = "ProgressBar Example";
progressBar1.Parent = this;
progressBar1.Location = new System.Drawing.Point(50, 50);
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Width = 200;
//...
progressBar2.Parent = this;
progressBar2.Location = new System.Drawing.Point(50, 80);
progressBar2.Style = ProgressBarStyle.Continuous;
progressBar2.Value = 50;
progressBar2.Width = 200;
progressBar3.Parent = this;
progressBar3.Location = new System.Drawing.Point(50, 110);
progressBar3.Maximum = 300;
progressBar3.Style = ProgressBarStyle.Continuous;
progressBar3.Value = 300;
progressBar3.Width = 200;
progressBar4.Parent = this;
progressBar4.Location = new System.Drawing.Point(50, 140);
progressBar4.Style = ProgressBarStyle.Continuous;
progressBar4.Maximum = 140;
progressBar4.Style = ProgressBarStyle.Continuous;
progressBar4.Width = 200;
progressBar5.Parent = this;
progressBar5.Location = new System.Drawing.Point(50, 170);
progressBar5.Style = ProgressBarStyle.Marquee;
progressBar5.Width = 200;
timer.Interval = 50;
timer.Enabled = true;
timer.Tick += delegate (object sender, EventArgs e) {
progressBar4.Value = progressBar4.Value < progressBar4.Maximum ? progressBar4.Value + 1 : progressBar4.Minimum;
};
}
private ProgressBar progressBar1 = new ProgressBar();
private ProgressBar progressBar2 = new ProgressBar();
private ProgressBar progressBar3 = new ProgressBar();
private ProgressBar progressBar4 = new ProgressBar();
private ProgressBar progressBar5 = new ProgressBar();
private Timer timer = new Timer();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
Example 2: C# Windows Forms ProgressBar Example
A step by step example to help you learn ProgressBar, which is a
System.Windows.Forms
component for rendering progress state of an operation.
This tutorial will help you learn the following concepts:
- How to create a ProgressBar in Windows Forms application.
- How to create a
Continuous
andMarquee
ProgressBar in windows forms. - How to create a simple Timer with ProgressBar
- How to Update a ProgressBar with a
Timer
component.
This example will demonstrate how create multiple progressbars programmatically and update them via Timer
component.
This example will comprise the following files:
ProgressBar2.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project
Step 2: Write Code
Write Code as follows:
*(a). ProgressBar2.cs
Create a file named ProgressBar2.cs
Start by adding the following imports. The System.Windows.Forms
will host our ProgressBar
while the System.Drawing
will allow to us programmatically draw the ProgressBar at a specified location on our screen.
using System;
using System.Drawing;
using System.Windows.Forms;
Create a namespace
known as Examples
:
namespace Examples {
Create our class
and extend the Form
class from the System.Windows.Forms
:
class Form1 : Form {
Crreate our main
method. Inside it EnableVisualStyles()
and Run()
our application.
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1 {Text = "ProgressBar2 Example"});
}
Define our constructor and add several ProgressBar
widgets to the Controls
property of the Form
via the Add()
method. Specify the ProgressStyle
, Width, value etc. In this case are demonstrating the ProgressBarStyle.Marquee
and ProgressBarStyle.Continuous
styles.
public Form1() {
this.Controls.Add(new ProgressBar {Location = new Point(50, 50), Style = ProgressBarStyle.Continuous, Width = 200});
this.Controls.Add(new ProgressBar {Location = new Point(50, 80), Style = ProgressBarStyle.Continuous, Width = 200, Value = 50});
this.Controls.Add(new ProgressBar {Location = new Point(50, 110), Style = ProgressBarStyle.Continuous, Width = 200, Maximum = 300, Value = 300});
this.Controls.Add(new ProgressBar {Location = new Point(50, 140), Style = ProgressBarStyle.Continuous, Width = 200, Maximum = 140});
this.Controls.Add(new ProgressBar {Location = new Point(50, 170), Style = ProgressBarStyle.Marquee, Width = 200});
Instantiate a Timer
object, set the interval and Enabled
property:
Timer timer = new Timer {Interval = 50, Enabled = true};
Handle the Timer tick property and do update the value of the ProgressBar
:
timer.Tick += (sender, e) => (this.Controls[3] as ProgressBar).Value = (this.Controls[3] as ProgressBar).Value < (this.Controls[3] as ProgressBar).Maximum ? (this.Controls[3] as ProgressBar).Value + 1 : (this.Controls[3] as ProgressBar).Minimum;
Here is the full code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1 {Text = "ProgressBar2 Example"});
}
public Form1() {
this.Controls.Add(new ProgressBar {Location = new Point(50, 50), Style = ProgressBarStyle.Continuous, Width = 200});
this.Controls.Add(new ProgressBar {Location = new Point(50, 80), Style = ProgressBarStyle.Continuous, Width = 200, Value = 50});
this.Controls.Add(new ProgressBar {Location = new Point(50, 110), Style = ProgressBarStyle.Continuous, Width = 200, Maximum = 300, Value = 300});
this.Controls.Add(new ProgressBar {Location = new Point(50, 140), Style = ProgressBarStyle.Continuous, Width = 200, Maximum = 140});
this.Controls.Add(new ProgressBar {Location = new Point(50, 170), Style = ProgressBarStyle.Marquee, Width = 200});
Timer timer = new Timer {Interval = 50, Enabled = true};
timer.Tick += (sender, e) => (this.Controls[3] as ProgressBar).Value = (this.Controls[3] as ProgressBar).Value < (this.Controls[3] as ProgressBar).Maximum ? (this.Controls[3] as ProgressBar).Value + 1 : (this.Controls[3] as ProgressBar).Minimum;
}
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |