1. Submitted by: MUHAMMAD USMAN
SALEEM (543)
Submitted to: ZAHID ASLAM & WHOLE CLASS
Progress-Bar
2. INTRODUCTION
Progress-Bar indicates visually the progress of
an operation. It is best used on a long-running
computation or task. And the Background-Worker
is often used to perform that task—it does not
block the interface.
3. Continue...
Example: Please add a Progress-Bar control to
your Windows-Forms program by double-clicking
on the Progress Bar entry in the Toolbox. For this
example, we also need a Background Worker—
add one and then create the event handlers
required.
Note : We need the Load event handler, the Do
Work event handler, and the Progress Changed
event handler.
4. Example that uses Progress Bar
and Background Worker: C#
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
6. Continue…
private void backgroundWorker1_ProgressChanged(object
sender, ProgressChangedEventArgs e)
{
// Change the value of the ProgressBar to the
BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
// Set the text.
this.Text = e.ProgressPercentage.ToString();
}
}
}
7. Starting the background task
In the Form1_Load event handler, please add a
method call to the Run-Worker-Async method on
the Background-Worker instance. This will create
a thread and the Do-Work method will begin.
8. METHOD
Inside the Do-Work event handler, we loop
over the numbers 1 through 100. At each
iteration, we wait one-tenth of a second. Then, we
call the Report-Progress method on the
Background-Worker instance.
And:
In Progress-Changed, we set the value of the
Progress-Bar to the Progress-Percentage
property of the Progress-Changed-EventArgs
argument.
9. PROPERTY
Color
One of the useful properties on the Progress-Bar
is the Fore-Color property. This can be set to a
Color value to change the color of the bar itself
that is drawn on the screen. It is often better to
use the default color.
Note:In this screenshot, we see a yellow bar
instead of the default blue bar. This is likely a bad
design decision.
10. Summary
The Progress-Bar control is often used in
conjunction with a threading mechanism such as
Background-Worker. With its many options, the
Progress-Bar makes a suitable widget for letting
your users know the application is still active.