Click here to Skip to main content
15,887,967 members
Articles / Programming Languages / C#
Article

C# Progress Bar Status Bar Panel

Rate me:
Please Sign up or sign in to vote.
4.21/5 (18 votes)
30 Jun 20041 min read 293.1K   4.4K   59   21
A reusable progress bar panel control for display inside the status bar.

Progress Bar In Use

Introduction

You can't just drop a progress bar control into a status bar panel, so I made this. This is actually the second time I've had to do this, so I got fed up and wanted to drop the code here so nobody would have to do it again.

Using the code

First thing to do is just drop a status bar on your form, and create a Panel or two. You can mess around with the panel settings afterwards, but the easiest thing to do is select Owner-Draw on the status bar panel that you want to be the progress bar from the very beginning.

C#
// Change the type of the panel to StatusBarProgressPanel on your Form
private StatusBarProgressPanel progressPanel;

// Initialize the measurement values
progressPanel.StartPoint = 0;
progressPanel.EndPoint = 100;
progressPanel.StepSize = 1;
progressPanel.ProgressPosition = 0;

Of course, once you change the type, you can change the properties using the Visual Studio Designer by simply clicking the "..." button of the Panel's property when the status bar is selected in the form designer. I've included some handy category attributes for Visual Studio's nifty reflection logic to pick up.

Properties Dialog from Status Bar in Form Designer

C#
// Sample usage:

// progress the bar by one "step"
progressPanel.Step();

// reset the progress bar to the initial state
progressPanel.Reset();

// start/stop an infinite animation thread for the bar
// this is useful when you don't know how long something
// is going to take, or if the progress is just unmeasurable
progressPanel.StartAnimation();
progressPanel.StopAnimation();

// By default the value of the percentage of progress is displayed
// setting the "ShowText" property to false disables it
progressPanel.ShowText = false;

You may also change the AnimationStyle of the control. This basically controls the way the indicator is filled and emptied. The default is ProgressDisplayStyle.Infinate which is a pulsating draw style. Much cooler graphics can be used, just modify the Parent_DrawItem method.

C#
/// <summary>
/// Statusbar Progress Display Styles
/// </summary>
public enum ProgressDisplayStyle
{
    /// <summary>
    /// A continually moving animation
    /// </summary>
    Infinite,
    /// <summary>
    /// A progress bar that fills from left to right
    /// </summary>
    LeftToRight,
    /// <summary>
    /// A progress bar that fills from right to left
    /// </summary>
    RightToLeft,
    /// <summary>
    /// A progress bar that fills from bottom to top
    /// </summary>
    BottomToTop,
    /// <summary>
    /// A progress bar that fills from top to bottom
    /// </summary>
    TopToBottom
}

History

  • 7/1/2004 - Initial revision.

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Burak Tunçbilek18-Jun-12 10:23
Burak Tunçbilek18-Jun-12 10:23 
Questionnew Pin
Aslam Jarwar3-Mar-12 22:01
Aslam Jarwar3-Mar-12 22:01 
GeneralMy vote of 1 Pin
ch@ndr@ mouli4-Aug-11 23:31
ch@ndr@ mouli4-Aug-11 23:31 
Generalanother stylish progress bar Pin
Ramesh Soni7-Jul-07 22:28
Ramesh Soni7-Jul-07 22:28 
GeneralNicely done Pin
Kevin Sanders25-Nov-05 13:00
Kevin Sanders25-Nov-05 13:00 
GeneralUpdating while working Pin
RyanRushaConto10-Jul-05 14:50
RyanRushaConto10-Jul-05 14:50 
GeneralRe: Updating while working Pin
ww_b8-Nov-06 0:05
ww_b8-Nov-06 0:05 
GeneralForcing Style=Owner-Draw Pin
Mike Kiefer1-Jun-05 14:47
Mike Kiefer1-Jun-05 14:47 
GeneralprogressPosition update doesn't update the bar Pin
winryan12-Mar-05 10:13
winryan12-Mar-05 10:13 
GeneralThanks + small bug fix . Pin
Anonymous18-Jul-05 7:46
Anonymous18-Jul-05 7:46 
GeneralRe: Thanks + small bug fix . [modified] Pin
__alias13-Sep-06 6:19
__alias13-Sep-06 6:19 
Thanks for this control it saved my bacon when I had to downgrade a .NET2 statusStrip to a .NET1 statusBar (For custom skinning reasons)

After implementing the above posts modifications I was getting an exception thrown of 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.' when I called the following line

this.Parent.Invoke( _refreshDelegate );

This was due to the fact that something in my program was trying to use the control before the main form had been fully initialised. Fortunately with a little addition to this control it was easy to fix.

First of all modify the ProgressPosition assessor so that it is

[Category("Measurement")]<br />
public long ProgressPosition<br />
{<br />
get<br />
{<br />
return _currentPosition;<br />
}<br />
set<br />
{<br />
_currentPosition = value;<br />
if (this.Parent != null && _isHandleCreated == true)<br />
this.Parent.Invoke( _refreshDelegate );<br />
}<br />
}


Then add the following code to the control

private bool _isHandleCreated = false;<br />
        [Category("Crash Protection")]<br />
        public bool IsHandleCreated<br />
        {<br />
            set<br />
            {<br />
                _isHandleCreated = value;<br />
            }<br />
        }


Finally add something like this code to the form that the control is on

public Form1()<br />
{<br />
InitializeComponent();<br />
this.HandleCreated += new EventHandler(Form1_HandleCreated);<br />
}<br />
<br />
        void Form1_HandleCreated(object sender, EventArgs e)<br />
        {<br />
            statusBarPanel2.IsHandleCreated = true;<br />
            //throw new Exception("The method or operation is not implemented.");<br />
        }


I found this way easier to do than trawling through the code trying to find out what had set off the progress bar on the status bar early.



-- modified at 12:27 Wednesday 13th September, 2006
GeneralDouble Buffer Progress Pin
CoreyRIT8-Dec-04 10:47
CoreyRIT8-Dec-04 10:47 
GeneralRe: Double Buffer Progress Pin
hatton22-Dec-04 1:15
hatton22-Dec-04 1:15 
GeneralPanel turns white Pin
Vrungar2-Aug-04 11:09
Vrungar2-Aug-04 11:09 
QuestionHow to use your 'StatusBarProgressPanel' in design mode directly? Pin
w1424311-Jul-04 14:53
w1424311-Jul-04 14:53 
AnswerRe: How to use your 'StatusBarProgressPanel' in design mode directly? Pin
steven_fusco12-Jul-04 4:42
steven_fusco12-Jul-04 4:42 
GeneralRe: How to use your 'StatusBarProgressPanel' in design mode directly? Pin
Christian Wikander9-Aug-04 21:54
Christian Wikander9-Aug-04 21:54 
GeneralRe: How to use your 'StatusBarProgressPanel' in design mode directly? Pin
rsil5-Oct-04 21:10
rsil5-Oct-04 21:10 
GeneralRe: How to use your 'StatusBarProgressPanel' in design mode directly? Pin
mkiefer1-Jun-05 15:04
mkiefer1-Jun-05 15:04 
GeneralGreat work Pin
windcbf6-Jul-04 21:24
windcbf6-Jul-04 21:24 
GeneralFinally Pin
Mr. Rogers6-Jul-04 11:41
Mr. Rogers6-Jul-04 11:41 

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.