一个可以自动调整大小的工具条

时间:2010-06-22 04:17:29

标签: c# winforms toolstrip

我正在使用这个很好的代码,顺便说一句,如果你知道有任何更好的方法来完成这个,我真的很感激让我们知道。 所以这里是可以浮动的工具栏:

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

很好,但是如果我在这个工具栏上只有4个按钮,当我将它浮动时,它仍然与之前停靠在表单上的大小相同但是我希望它可以自己调整大小并且只要它就可以了需要在其上显示其按钮。

3 个答案:

答案 0 :(得分:1)

您可以添加各个工具条项目的宽度,并将其用作表单的宽度。

替换它:

floatForm.ClientSize = this.Size;

用这个:

//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);

答案 1 :(得分:1)

  m_floatForm.AutoSize = True
  m_floatForm.AutoSizeMode = AutoSizeMode.GrowAndShrink

答案 2 :(得分:0)

PreferredSize为我做了诀窍。我没有期望在ToolStip上工作,但至少在.Net 4.5上工作。

我仍然需要添加一个固定的数字来计算几个像素,我不知道它们来自哪里。

this.Width = toolStrip.PreferredSize.Width + toolStrip.Margin.Horizontal + toolStrip.Parent.Margin.Horizontal + toolStrip.Parent.Padding.Horizontal+20;
相关问题