Home > WinForms > C#: WinForms – How to expand dynamically width of ToolStripProgressBar to fill entire free space in StatusStrip

C#: WinForms – How to expand dynamically width of ToolStripProgressBar to fill entire free space in StatusStrip

17:23

Marek Śliwiński Leave a comment Print This Post  (1,260) Go to comments

I often would like to have ProgressBar (hidden and shown only while some background application work is doing) in my StatusStrip. My expectation is the ProgressBar should always fill entire available free space in StatusStrip, when form is Maximizing, Resizing etc. This kind of behaviour can’t be set in Visual Studio Designer with control properties settings. Below is my solution to solve this problem. If you know better, simplest way then please let me know by your comment :)

// Method to calculate suitable ToolStripProgressBar width
private int CalculateControlSizeInStatusStrip(StatusStrip statusStrip,
                         Control statusProgressBar)
{
    int width = statusStrip.ClientSize.Width;
    int gripWidth = 20;

    if (statusStrip.Items.Count > 1)
    {   // Calculate width for progress bar if more controls
        // on StatusStrip
        for (int i = 0; i < statusStrip.Items.Count - 1; i++)
        {
            // width calculation
            width -= statusStrip.Items[i].Width;
        }
    }

    return width - gripWidth;
}

on your StatusStrip Resize event:

private void statusStripMain_Resize(object sender, EventArgs e)
{
    // Expand dynamically width of ToolStripProgressBar to
    // fill entire free space in StatusStrip
    toolStripStatusProgressBarMainTask.ProgressBar.Width = CalculateControlSizeInStatusStrip((StatusStrip)sender, toolStripStatusProgressBarMainTask);
}
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: WinForms Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Subscribe without commenting