更改动态添加的 ToolStripItems 的 ToolStripPanel 溢出行为

发布于 2024-07-14 06:06:32 字数 1560 浏览 5 评论 0 原文

我有一个 Windows 窗体 ToolStripPanel,我可以向其中动态添加新的 ToolStrip。 每个 ToolStrip 都有我动态添加到其中的 ToolStripItems(主要是 ToolStripButtons)。

我希望将所有 ToolStrip 放在一行中,只要您可以看到每个 ToolStrip 中的所有 ToolStripItem。 如果向 ToolStrip 添加新的 ToolStripItem 导致发生溢出,我希望行末尾的 ToolStrip 向下跳转到新行。 我唯一希望看到溢出按钮的情况是 ToolStrip 有太多项目以至于无法适应表单的宽度。

我希望这是默认行为,除非用户手动移动 ToolStrip。 如果用户移动 ToolStrip,那么我希望发生现有的溢出(例如带有下拉选项的溢出按钮)行为。 我假设如果用户自己定位工具栏,他们会接受这种默认的 Winforms 溢出行为。

如果在用户重新定位事物后添加新的动态工具栏,我希望将新工具栏添加到底行最后一个 ToolStrip 的右侧。 该工具栏应该具有更新的溢出行为(例如,如果无法容纳,则使用新行),直到用户手动重新定位它。

我做了一些研究,但没有取得巨大成功:

我是否缺少一个简单的解决方案?

更新:仍在寻找好的解决方案。 看来 LayoutCompleted 路线是可能的。 但它的问题是它被多次调用。 有一次它说 ToolStripItem 位于溢出区域,然后立即位于主区域,因此仅检查一次它是否处于溢出区域似乎并不安全。

I have a Windows Forms ToolStripPanel that I dynamically add new ToolStrips to it. Each ToolStrip has ToolStripItems that I dynamically add to it (mostly ToolStripButtons).

I would like to have all the ToolStrips fit on one row as long as you can see all the ToolStripItems in each ToolStrip. If adding a new ToolStripItem to a ToolStrip causes overflow to occur, I'd like the ToolStrip at the end of the row to jump down to a new row. The only time I'd like to see the overflow button is if the ToolStrip has so many items that they can't fit on the width of the form.

I want this to be the default behavior unless a user manually moves a ToolStrip. If the user moves the ToolStrip, then I want the existing overflow (e.g. overflow button with drop down choices) behavior to occur. I assume the user will be OK with this default Winforms overflow behavior if they position the toolbar themselves.

If a new dynamic toolbar is added after the user repositions things, I'd like the new toolbar to be added to the right of the last ToolStrip in the bottom row. This toolbar should have the updated overflow behavior (e.g. use a new row if you can't fit) until the user manually repositions it.

I've done some research without great success:

  • This seems like it should be a simple matter of changing a ToolStripLayoutStyle, but this doesn't seem to be the case. (At best, new rows are created when the overflow button is showing on all ToolStrips in a given row).
  • The MSDN article "How to: Manage ToolStrip Overflow in Windows Forms" seems to only cover if/when the overflow button occurs.
  • There is a LayoutCompleted event on the ToolStrip that seems possible to use to tweak things, it seems like there should be an easier way.
  • Creating a custom LayoutEngine seems like an overkill for this since the built-in one is very close to what I want to do.

Am I missing an easy solution?

UPDATE: Still looking for a good solution. It seems the LayoutCompleted route is possible. The problem with it though is that it is called multiple times. One time it says the ToolStripItem is in the overflow area and immediately after that is in the Main area, so it doesn't seem safe to just check one time if it's in overflow.

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

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

发布评论

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

评论(2

滿滿的愛 2024-07-21 06:06:32

1) 将项目添加到工具条

2) 将工具条的宽度与最后一行剩余的空间量进行比较。 ToolStripPanel 有一个 Rows[] 属性,它会告诉您最后一行是什么以及该行上有哪些工具条。 将它们的宽度相加,然后从面板的宽度中减去它们。

3) 使用 Join 方法将新工具条添加到面板,指定最后一行(如果适合)或更高的一行(如果不适合)。

1) Add the items to the toolstrip

2) Compare the width of the toolstrip to the amount of space left on the last row. The ToolStripPanel has a Rows[] property that will tell you what the last row is and what toolstrips are on that row. Add up their widths, and subtract them from the width of the panel.

3) Add the new toolstrip to the panel using the Join method, specifying either the last row (if it fits) or one row higher (if it doesn't).

东京女 2024-07-21 06:06:32

最后,我认为没有一个好的简单解决方案。 此主题 清楚地表明 ToolStrips 并不是真正为动态场景而设计的。

一种独特的方法是设置 ToolStrip 的 MinimumSize 是所有按钮的大小。 这将开始像我描述的那样处理溢出,但即使这样也存在边缘情况的问题。

我发现的最好方法是尽一切可能提前很好地了解工具栏的大小,然后使用 加入方法。 如果您提前做出合理的猜测,那么稍后添加更多按钮可能没问题,因为它们最终会出现在溢出区域中。

In the end, I don't think there is a good easy solution. This thread makes it clear that ToolStrips weren't really designed for dynamic scenarios as a priority.

One unique approach is to set the ToolStrip's MinimumSize to be the size of all the buttons. This will start to work with overflow like I describe, but even this has problems with edge cases.

The best approach I've found is to do everything possible to get a good feel for what the size of the toolbar should be in advance and then position it on the ToolStripPanel using the Join method. If you make a reasonable guess ahead of time, then it's probably ok if you add more buttons later as they'll end up in the overflow area.

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