尝试将 ToolStrip 与现有 ToolStrip 并排添加到 ToolStripPanel
我将 .net 2.0 与 Visual Studio 2005 结合使用,并尝试将两个不同的工具条添加到表单顶部,以便它们并排显示。 我希望它像 Word 2003 一样,您可以将多个工具条添加到同一行并让它们彼此对齐显示,而不是为每个工具条指定一行。
因此,我添加了一个 ToolStripPanel 并将其停靠到表单的顶部(我没有使用 ToolStripContainer,因为我不需要所有额外的面板;我只需要顶部的面板)。 我添加了两个工具条并将其 Stretch 属性设置为 False。 我可以让它们并排显示在设计器窗口中,但在运行时 ToolStripPanel 会分隔工具条并为每个工具条提供其自己的专用行。 好像雪上加霜的是,当我停止调试并返回设计师时,我发现设计师也将工具条移到了自己的行中! 我在这里做错了什么吗?
我整天都在谷歌搜索,找到了一些有关 ToolStripPanelRow 对象的信息,但我没有看到向其添加工具条的简单方法(即它没有 ToolStripPanelRow.Controls.Add 方法或类似的方法),所有它有一个 Controls() 属性,它返回一个控制对象数组,但我在尝试向该数组添加项目时运气不佳。 我还发现了一些关于 ToolStripPanel.Join 方法的文档,听起来它应该可以完成这项工作,所以我尝试了所有 3 个重载,但它们并不像宣传的那样工作。 无论我做什么或尝试哪些选项,它总是将新的工具条添加到面板顶部的一行中,并将其他所有内容推到下方。
为了充分披露,我应该警告您,我已将 ToolStripPanel 和其中一个工具条添加到基类表单中,并且我正在尝试将另一个工具条添加到从基类表单继承的子类表单中。 基类形式的 ToolStripPanel 和 ToolStrip 都声明为“Protected Friend”,因此这应该可以工作。 正如我所提到的,子类表单的设计器窗口将允许我这样做(至少暂时如此)。
如果有人能帮助我让这个工作正常工作,或者至少解释为什么它不能工作,我将非常感激。
I'm using .net 2.0 with Visual Studio 2005 and I am trying to add two different toolstrips to the top of the form such that they show up side-by-side. I want it to be like Word 2003, where you can add multiple toolstrips to the same row and have them show up in line with each other, rather than dedicating a row to each toolstrip.
So I added a ToolStripPanel and docked it to the top of the form (I didn't use a ToolStripContainer because I don't need all the extra panels; I just need the one at the top). I added both toolstrips and set their Stretch properties to False. I can get them to show up in the designer window side-by-side, but at runtime the ToolStripPanel separates the toolstrips and gives each toolstrip its own dedicated row. As if to add insult to injury, when i stop debugging and return back to the designer, I am finding that the designer is moving the toolstrips to their own row as well! Am I doing something wrong here?
I have been Googling all day and found some information about a ToolStripPanelRow object, but I don't see an easy way to add toolstrips to it (i.e. it doesn't have a ToolStripPanelRow.Controls.Add method or anything like that), all it has is a Controls() property that returns an Array of control objects, and I haven't had much luck trying to add items to that array. I also found some documentation on the ToolStripPanel.Join method, which sounds like it should do the job, so I tried all 3 overloads but they don't work as advertised. No matter what I do or which options I try, it always adds the new toolstrip to the top of the panel on its own row and pushes everything else down.
In the interests of full disclosure I should warn you that I have the ToolStripPanel and one of the toolstrips added to a baseclass form, and I am trying to add the other toolstrip to a subclass form that inherits from the baseclass form. The ToolStripPanel and ToolStrip in the baseclass form are both declared "Protected Friend", so this should be working. As I mentioned, the subclass form's designer window will allow me to do it (at least, for a time).
If anyone can help me get this working or at least shed some light on why it isn't, I would be extremely grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建了一个自定义 ToolStripPanel,以便可以重载 LayoutEngine;
然后自定义LayoutEngine对ToolStrips进行布局;
需要一段时间的一件事是,更改一个 ToolStrip 项的位置/大小将导致布局重新触发,控件重新排序。 所以我在布局循环之前复制了控件。
由于某种原因,您无法使用 AddRange(...) 将项目添加到自定义面板 - 需要一次添加一个项目。
希望有所帮助(它基于 MSDN LayoutEngine 示例,针对 ToolStripPanel 进行了修复)
Wyzfen
I created a custom ToolStripPanel so that I could overload the LayoutEngine;
Then the custom LayoutEngine lays out the ToolStrips;
One thing that took a while is that changing the location / size of one ToolStrip item will cause the layout to re-fire, with the controls reordered. So I take a copy of the controls before the layout loop.
And you cant use AddRange(...) to add items to the Custom Panel for some reason - need to Add(...) them one at a time.
hope that helps (it's based on MSDN LayoutEngine Example, fixed for ToolStripPanels)
Wyzfen
System.Windows.Forms.FlowLayoutPanel 可以完成这项工作。
只需将 ToolStrip 控件按正确的顺序放入其中即可。
System.Windows.Forms.FlowLayoutPanel can do the job.
Just put the ToolStrip controls in it with correct order.
我认为在您的情况下,您只需将 ToolStrip 上的 LayoutStyle 设置为 ToolStripLayoutStyle.HorizontalStackWithOverflow 即可摆脱困境,而不需要自己的自定义 LayoutEngine。
我就类似的主题提出了一个不同的问题,关于如何使用动态项目处理布局,以更好地控制溢出。
I think in your case you can get away with just setting the LayoutStyle on your ToolStrip to ToolStripLayoutStyle.HorizontalStackWithOverflow rather than need your own custom LayoutEngine.
I asked a different question on a similar topic on how to handle layout with dynamic items to have better control of the overflow.