C#: WinForms – How to expand dynamically width of ToolStripProgressBar to fill entire free space in StatusStrip
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);
}

(1,260)

