Search Amazon:

Add a ProgressBar to a derived StatusBar in C#

You can add a ProgressBar to a panel on your .NET StatusBar control by subclassing the StatusBar. Then you use the derived StatusBar on your WinForm just as you would use the standard control.

Subclass the StatusBar control by creating a new class:

    using System;
    using System.Windows.Forms;

namespace ProgStat
{
    public class ProgressStatus : StatusBar
    {
        private int _progressBarPanel = -1;
        public ProgressBar progressBar = new ProgressBar();

        public ProgressStatus()
        {
            progressBar.Hide();
            this.Controls.Add(progressBar);
            this.DrawItem += new StatusBarDrawItemEventHandler(this.Reposition);
        }

        public int setProgressBarPanel
        {
            //
            // Property to tell the StatusBar which panel to use for the ProgressBar.
            //
            get { return _progressBarPanel; }
            set 
                {
                _progressBarPanel = value;
                this.Panels[_progressBarPanel].Style = StatusBarPanelStyle.OwnerDraw;
                }
        }

        private void Reposition(object sender, StatusBarDrawItemEventArgs sbdevent)
        {
            progressBar.Location = 
               new System.Drawing.Point(sbdevent.Bounds.X, sbdevent.Bounds.Y);
            progressBar.Size = 
               new System.Drawing.Size(sbdevent.Bounds.Width, sbdevent.Bounds.Height);
            progressBar.Show();
        }
    }
}

In this sample the StatusBar has 2 panels and the second panel, pnlProgress, contains the ProgressBar. Here is the code to add the new control to your form. You could also add a standard StatusBar and modify it axccordingly.

Add the derived StatusBar to your form. Add this to the InitializeComponent section:

private void InitializeComponent()
{
    ...
    this.statusBar = new ProgressStatus();
    this.pnl1 = new System.Windows.Forms.StatusBarPanel();
    this.pnlProgress = new System.Windows.Forms.StatusBarPanel();
        ((System.ComponentModel.ISupportInitialize)(this.pnl1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pnlProgress)).BeginInit();
    this.SuspendLayout();
    // 
    // statusBar
    // 
    this.statusBar.Location = new System.Drawing.Point(0, 407);
    this.statusBar.Name = "statusBar";
    this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] 
        {this.pnl1,this.pnlProgress});
    this.statusBar.setProgressBarPanel = -1;
    this.statusBar.ShowPanels = true;
    this.statusBar.Size = new System.Drawing.Size(584, 22);
    this.statusBar.TabIndex = 0;
    // 
    // pnl1
    // 
    this.pnl1.MinWidth = 100;
    // 
    // pnlProgress
    // 
    this.pnlProgress.AutoSize = 
          System.Windows.Forms.StatusBarPanelAutoSize.Spring;
    this.pnlProgress.Style = 
          System.Windows.Forms.StatusBarPanelStyle.OwnerDraw;
    this.pnlProgress.Width = 468;
    // 
    // Form1
    // 
    ...
    this.Controls.Add(this.statusBar);
    ((System.ComponentModel.ISupportInitialize)(this.pnl1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.pnlProgress)).EndInit();
    this.ResumeLayout(false);
    ...
}

Add these module level declarations:

    private ProgressStatus statusBar = new ProgressStatus();
    private System.Windows.Forms.StatusBarPanel pnl1;
    private System.Windows.Forms.StatusBarPanel pnlProgress;

Add this code to use the derived StatusBar:

    //
    // Set properties of the derived statusbar.
    //
    this.statusBar.progressBar.Minimum = 0;
    this.statusBar.progressBar.Maximum = 100;
    this.statusBar.setProgressBarPanel = 1;
    ...
    this.statusBar.Panels[0].Text = "Panel 1 text";
    ...
    //
    // In your code loop, increment the ProgressBar.
    //
    this.statusBar.progressBar.Value += 1;
Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms