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

发布于 2024-09-06 16:35:52 字数 388 浏览 1 评论 0原文

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

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

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

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 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初相遇 2024-09-13 16:35:52
  m_floatForm.AutoSize = True
  m_floatForm.AutoSizeMode = AutoSizeMode.GrowAndShrink
  m_floatForm.AutoSize = True
  m_floatForm.AutoSizeMode = AutoSizeMode.GrowAndShrink
何其悲哀 2024-09-13 16:35:52

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

将其替换

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

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);
天邊彩虹 2024-09-13 16:35:52

PreferredSize 帮我解决了这个问题。我没想到会在 ToolStip 上工作,但它确实如此,至少在 .Net 4.5 上是这样。

我仍然必须添加一个固定的数字来说明一些我不确定它们来自哪里的像素。

this.Width = toolStrip.PreferredSize.Width + toolStrip.Margin.Horizontal + toolStrip.Parent.Margin.Horizontal + toolStrip.Parent.Padding.Horizontal+20;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文