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)
{
if (isAdded == false)
{
this.Parent.Controls.Add(this.progressBar);
this.isAdded = true;
}
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:
- Create your
StatusBar
and add it to the form.
- 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
.
- Close the
StatusBarPanel
Collection Editor window.
- View the source code for the form that contains the
StatusBar
.
- Change the progress panel's class from
System.Windows.Form.StatusBarPanel
to MarkHarmon.Controls.StatusBarProgressPanel
.
- Set the status bar's
DrawItem
event handler to the StatusBarProgressPanel
's ParentDrawItemHandler
method.
- Save and rebuild the project.
- 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:
statusBar1.DrawItem +=
new StatusBarDrawItemEventHandler(progressPanel.ParentDrawItemHandler);
To set the progress bar's position or other properties programmatically, just use the panel's ProgressBar
property.