Click here to Skip to main content
Click here to Skip to main content

C# StatusBarProgressPanel Control

, 26 Apr 2005
Rate this:
Please Sign up or sign in to vote.
A status bar panel that displays a standard ProgressBar control.

Introduction

I wanted a progress bar for my status bar and wasn't satisfied with any of the code found on the internet. My solution is to create a user control inherited from the StatusBarPanel class and add a ProgressBar control as a member object.

The source code in the demo project is straightforward and doesn't require much explanation here. I will give you a brief overview of the StatusBarProgressPanel class and how to use it in your own project.

Class Basics

The only difference between a StatusBarPanel and my class is the addition of the ProgressBar property.

private ProgressBar progressBar = new ProgressBar();
[Category("Progress")]
public ProgressBar ProgressBar
{  
  get { return progressBar; }
}

...and an event handler for the status bar's DrawItem event.

public void ParentDrawItemHandler(object sender, 
                       StatusBarDrawItemEventArgs sbdevent)
{
  // Only add this once to the parent's control container
  if (isAdded == false)
  {
    this.Parent.Controls.Add(this.progressBar);
    this.isAdded = true;
  }
    
  // Get the bounds of this panel and copy to the progress bar's bounds
  if (sbdevent.Panel == this)
    progressBar.Bounds = sbdevent.Bounds;
}

I know it may not be the most efficient way to do things, but it works and it's simple. If anyone wants to post any improvements, I will be happy to add them and give you the credit.

How to use

Using the progress panel is really simple. The steps involved are as follows:

  1. Create your StatusBar and add it to the form.
  2. Open up the StatusBarPanel Collection Editor from the Properties window. Click on the Add button to add a new panel and set the panel's Style to OwnerDraw.
  3. Close the StatusBarPanel Collection Editor window.
  4. View the source code for the form that contains the StatusBar.
  5. Change the progress panel's class from System.Windows.Form.StatusBarPanel to MarkHarmon.Controls.StatusBarProgressPanel.
  6. Set the status bar's DrawItem event handler to the StatusBarProgressPanel's ParentDrawItemHandler method.
  7. Save and rebuild the project.
  8. Open the StatusBarPanel Collection Editor again and find the ProgressBar property. Set the progress bar's properties to your liking.

Here is an example of how to set the DrawItem event handler:

// Set the DrawItem event handler 
  statusBar1.DrawItem += 
    new StatusBarDrawItemEventHandler(progressPanel.ParentDrawItemHandler);

To set the progress bar's position or other properties programmatically, just use the panel's ProgressBar property.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Mark Harmon
Architect Deacon Software
United States United States
No Biography provided

Comments and Discussions

 
GeneralDouble Border Pinmemberianhowie8-Oct-05 8:20 
The statement
 
progressBar.Bounds = sbdevent.Bounds;
 
appears to result in the border of the ProgessBar being drawn inside the border of the Panel. Thus, for example, the height of the interior of the ProgressBar 'panel' is always a couple of pixels less than other Panels on the StatusBar.
 
It's possible to fix this by expanding the bounds of the ProgessBar in every direction but is there a more elegant way?
Generalsupporting repainting PinmemberDilwyn30-Sep-05 6:05 
GeneralGreat and efficient code PinmemberRyanDeveloper23-Sep-05 5:43 
GeneralRe: Great and efficient code PinmemberMark Harmon25-Sep-05 6:35 
GeneralVisual Designer problem Pinmemberrobmcbean22-Aug-05 13:49 
GeneralRe: Visual Designer problem PinmemberMark Harmon25-Sep-05 6:38 
GeneralShowPanels Pinmemberjharding22-Jun-05 6:00 
QuestionAdding the control twice? PinmemberUnquick22-Apr-05 12:39 
AnswerRe: Adding the control twice? PinmemberMark Harmon22-Apr-05 12:56 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 27 Apr 2005
Article Copyright 2005 by Mark Harmon
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid