This article demonstrates creating a progress indicator control, usually used in displaying the progress in populating a large number of data.
- 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
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.
void _progressTimer_Tick(object sender, EventArgs e)
if (ActiveIndex == this.DotsCount)
this.ActiveIndex = 0;
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.
while (iterator < this.DotsCount)
if (iterator == this.ActiveIndex)
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:
Stream imgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
this._activeDot = Image.FromStream(imgStream);
(this._activeDot as Bitmap).MakeTransparent(Color.White);
imgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
this._inactiveDot = Image.FromStream(imgStream);
(this._inactiveDot as Bitmap).MakeTransparent(Color.White);
Show method is invoked, the control is added to the
WrapperForm and filled to its size and shown as a dialog window.
public void ShowProgressIndicator()
this.WrapperForm.Size = this.Size;
this.Dock = DockStyle.Fill;
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.
protected override void Dispose(bool disposing)
_progressTimer.Tick -= new EventHandler(_progressTimer_Tick);
if (_wrapperForm != null)
_wrapperForm.Shown -= new EventHandler(wrapperFormShown);
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.
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000008 | 0x08000000;
Hope this helps you in building a rich user interface. Please do leave your valuable comments and suggestions.