1

I am using this nice code which by the way if you are aware of any better way to accomplish this, I really appreciate letting us know . so here is the Toolbar that can float :

http://en.csharp-online.net/Tool,_Menu,_and_Status_Strips%E2%80%94Floating_ToolStrips

good, but what if I only have 4 buttons on this toolbar, when I make it float it is still the same size as it was docked to the form before but I wish it could resize itself and just be as long as it needs to be to show its buttons on it .

Bohn
  • 26,091
  • 61
  • 167
  • 254

3 Answers3

1

You can add up the widths of the individual toolstrip items and use that as the width of your form.

Replace this:

floatForm.ClientSize = this.Size;

with this:

//Adjust min value for your needs. It should account for the width of the
//toolstrip, borders, etc.
int minWidth = 20;  

int newWidth = minWidth;
foreach (ToolStripItem item in this.Items)
{
    newWidth += item.Size.Width;
}
floatForm.ClientSize = new Size(newWidth, this.Size.Height);
msergeant
  • 4,771
  • 3
  • 25
  • 26
1
  m_floatForm.AutoSize = True
  m_floatForm.AutoSizeMode = AutoSizeMode.GrowAndShrink
serhio
  • 28,010
  • 62
  • 221
  • 374
0

PreferredSize did the trick for me. I didn't expect to work on a ToolStip but it does, at least on .Net 4.5.

I still had to add a fixed number to account for a few pixels that I'm not sure where they are coming from.

this.Width = toolStrip.PreferredSize.Width + toolStrip.Margin.Horizontal + toolStrip.Parent.Margin.Horizontal + toolStrip.Parent.Padding.Horizontal+20;
eperales
  • 468
  • 5
  • 13