Click here to Skip to main content
15,881,092 members
Articles / Desktop Programming / Windows Forms

Progress Indicator - Customizable

Rate me:
Please Sign up or sign in to vote.
4.78/5 (6 votes)
31 May 2011CPOL2 min read 34.7K   2.1K   28   6
This article demonstrates a customizable busy progress indicator for Windows Forms applications.

 

 

Introduction 

This article demonstrates creating a progress indicator control, usually used in displaying the progress in populating a large number of data.

Background

  • The key is to create active and inactive images, and to place them appropriately to make it appear animated.
  • A timer running in the background updates the position of the active image in a consistent duration.
  • Control is designed to be customizable in the designer, and can be shown as a modal dialog at runtime.
  • The images are usually of rectangular form and the background is ignored by a BitMap.MakeTransparent call.

Using the Code

The total number of images in the ProgressIndicator is controlled by the DotsCount property. A timer running on the background decides which index to be drawn active, and the rest of the indexes are painted inactive.

C#
void _progressTimer_Tick(object sender, EventArgs e)
{
    this.ActiveIndex++;

    if (ActiveIndex == this.DotsCount)
        this.ActiveIndex = 0;

    this.Refresh();
}

And in the paint event, active and inactive images are drawn; each image will be placed at a distance calculated based on the width of the control and the images.

C#
while (iterator < this.DotsCount)
{
    if (iterator == this.ActiveIndex)
    {
        e.Graphics.DrawImageUnscaled(this.ActiveImage,new Point(X,Y));
    }
    else
    {
        e.Graphics.DrawImageUnscaled(this.InActiveImage,new Point(X,Y));
    }
}

Default active and inactive images are added to the assembly as embedded resources, and populated to the property when the control is instantiated, as follows:

C#
Stream imgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
                   "Creatives.Resources.ActiveImage.png");
this._activeDot = Image.FromStream(imgStream);
(this._activeDot as Bitmap).MakeTransparent(Color.White);
            
imgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
                     "Creatives.Resources.InActiveImage.png");
this._inactiveDot = Image.FromStream(imgStream);
(this._inactiveDot as Bitmap).MakeTransparent(Color.White);

When the Show method is invoked, the control is added to the WrapperForm and filled to its size and shown as a dialog window.

C#
public void ShowProgressIndicator()
{
    this.WrapperForm.Controls.Add(this);
    this.WrapperForm.Size = this.Size;
    this.Dock = DockStyle.Fill;

    this.WrapperForm.ShowDialog();
}

Points of Interest

Every event hooked must be unhooked to avoid the object being held in memory to avoid memory leaks, and any unmanaged resources being used must be released.

C#
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        _progressTimer.Tick -= new EventHandler(_progressTimer_Tick);

        if (_wrapperForm != null)
        {
            _wrapperForm.Shown -= new EventHandler(wrapperFormShown);
        }
    }

    base.Dispose(disposing);
}

The wrapper form will not receive mouse activation, and remains on top of all the other windows; this is done with the help of window styles.

C#
CreateParams cp = base.CreateParams;

cp.ExStyle |= /*WS_EX_TOPMOST */0x00000008 | /*WS_EX_NOACTIVATE */0x08000000;

// Take a look at this link for more window styles
// http://www.pinvoke.net/default.aspx/Enums/WindowStylesEx.html

Hope this helps you in building a rich user interface. Please do leave your valuable comments and suggestions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
I code, learn, read and listen.

Comments and Discussions

 
QuestionUnable to download the source or demo Pin
Member 422724827-Nov-13 16:11
Member 422724827-Nov-13 16:11 
AnswerRe: Unable to download the source or demo Pin
VallarasuS28-Nov-13 7:32
VallarasuS28-Nov-13 7:32 
GeneralMy vote of 4 Pin
Yvan Rodrigues7-Jun-11 3:25
professionalYvan Rodrigues7-Jun-11 3:25 
GeneralMy vote of 5 Pin
Monjurul Habib6-Jun-11 21:01
professionalMonjurul Habib6-Jun-11 21:01 
GeneralShort article Pin
Slacker00731-May-11 5:02
professionalSlacker00731-May-11 5:02 
GeneralRe: Short article Pin
VallarasuS1-Jun-11 7:13
VallarasuS1-Jun-11 7:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.