【问题标题】:Trying to add a ToolStrip to a ToolStripPanel side-by-side with an existing ToolStrip尝试将 ToolStrip 与现有 ToolStrip 并排添加到 ToolStripPanel
【发布时间】:2008-12-31 18:49:11
【问题描述】:

我将 .net 2.0 与 Visual Studio 2005 一起使用,并尝试在表单顶部添加两个不同的工具条,以便它们并排显示。我希望它像 Word 2003 一样,您可以在其中将多个工具条添加到同一行并让它们彼此对齐显示,而不是为每个工具条指定一行。

所以我添加了一个 ToolStripPanel 并将其停靠在表单的顶部(我没有使用 ToolStripContainer,因为我不需要所有额外的面板;我只需要顶部的那个)。我添加了两个工具条并将它们的 Stretch 属性设置为 False。我可以让它们并排显示在设计器窗口中,但在运行时 ToolStripPanel 将工具条分开并为每个工具条提供自己的专用行。好像雪上加霜,当我停止调试并返回给设计人员时,我发现设计人员也在将工具条移动到他们自己的行!我在这里做错了吗?

我整天都在谷歌上搜索,发现了一些关于 ToolStripPanelRow 对象的信息,但我没有看到向它添加工具条的简单方法(即它没有 ToolStripPanelRow.Controls.Add 方法或类似的方法),它只有一个 Controls() 属性,该属性返回一个控件对象数组,而我在尝试向该数组中添加项目时运气不佳。我还找到了一些关于 ToolStripPanel.Join 方法的文档,听起来它应该可以完成这项工作,所以我尝试了所有 3 个重载,但它们并没有像宣传的那样工作。无论我做什么或尝试哪个选项,它总是将新工具条添加到面板顶部的单独行,并将其他所有内容向下推。

为了全面披露,我应该警告您,我已将 ToolStripPanel 和其中一个工具条添加到基类表单中,并且我正在尝试将另一个工具条添加到从基类表单继承的子类表单中。基类表单中的 ToolStripPanel 和 ToolStrip 都被声明为“受保护的朋友”,所以这应该可以工作。正如我所提到的,子类表单的设计器窗口将允许我这样做(至少有一段时间)。

如果有人能帮助我完成这项工作,或者至少能说明为什么不能这样做,我将不胜感激。

【问题讨论】:

    标签: user-interface visual-studio-2005 .net-2.0 toolstrip toolstrippanel


    【解决方案1】:

    我创建了一个自定义 ToolStripPanel,以便我可以重载 LayoutEngine;

    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.Layout;
    
    namespace CustomGUI
    {
      class CustomToolStripPanel : ToolStripPanel
      {
        private LayoutEngine _layoutEngine;
    
        public override LayoutEngine LayoutEngine
        {
          get
          {
            if (_layoutEngine == null) _layoutEngine = new CustomLayoutEngine();
            return _layoutEngine;
          }
        }
    
        public override Size GetPreferredSize(Size proposedSize)
        {
          Size size = base.GetPreferredSize(proposedSize);
    
          foreach(Control control in Controls)
          {
            int newHeight = control.Height + control.Margin.Vertical + Padding.Vertical;
            if (newHeight > size.Height) size.Height = newHeight;
          }
    
          return size;
        }
      }
    }
    

    然后自定义LayoutEngine对ToolStrips进行布局;

    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.Layout;
    
    namespace CustomGUI
    {
      class CustomLayoutEngine : LayoutEngine
      {
        public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
        {
          Control parent = container as Control;
    
          Rectangle parentDisplayRectangle = parent.DisplayRectangle;
    
          Control [] source = new Control[parent.Controls.Count];
          parent.Controls.CopyTo(source, 0);
    
          Point nextControlLocation = parentDisplayRectangle.Location;
    
          foreach (Control c in source)
          {
            if (!c.Visible) continue;
    
            nextControlLocation.Offset(c.Margin.Left, c.Margin.Top);
            c.Location = nextControlLocation;
    
            if (c.AutoSize)
            {
              c.Size = c.GetPreferredSize(parentDisplayRectangle.Size);
            }
    
            nextControlLocation.Y = parentDisplayRectangle.Y;
            nextControlLocation.X += c.Width + c.Margin.Right + parent.Padding.Horizontal;
          }
    
          return false;
        }
      }
    }
    

    需要一段时间的一件事是,更改一个 ToolStrip 项目的位置/大小将导致布局重新触发,控件重新排序。所以我在布局循环之前复制了一个控件。 由于某种原因,您不能使用 AddRange(...) 将项目添加到自定义面板 - 需要一次添加(...)它们。

    希望有所帮助(它基于 MSDN LayoutEngine Example,已针对 ToolStripPanels 修复)

    维兹芬

    【讨论】:

    • 感谢您的回答!我没有机会尝试它,因为我们决定不做并排的工具条,但还是感谢您发布您的答案。我会记住的,以防我们改变主意。
    【解决方案2】:

    System.Windows.Forms.FlowLayoutPanel 可以胜任。

    只需按照正确的顺序将 ToolStrip 控件放入其中即可。

    【讨论】:

    • 感谢您的回答!如果它再次出现,我会尝试它。
    【解决方案3】:

    我认为在您的情况下,您只需将 ToolStrip 上的 LayoutStyle 设置为 ToolStripLayoutStyle.Horizo​​ntalStackWithOverflow,而不需要您自己的自定义 LayoutEngine。

    我向different question 询问了关于如何处理动态项目的布局以更好地控制溢出的类似主题。

    【讨论】:

    • 感谢您的回答;我更多地询问的是同一个 ToolStripPanel 中多个 ToolStrips 的布局,而不是 ToolStrip 中 ToolStripItems 的布局(这对我来说一直很有效)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多