尝试将 ToolStrip 与现有 ToolStrip 并排添加到 ToolStripPanel

发布于 2024-07-10 19:43:58 字数 948 浏览 11 评论 0原文

我将 .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 技术交流群。

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

发布评论

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

评论(3

执手闯天涯 2024-07-17 19:43:58

我创建了一个自定义 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 示例,针对 ToolStripPanel 进行了修复)

Wyzfen

I created a custom ToolStripPanel so that I could overload the 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;
    }
  }
}

Then the custom LayoutEngine lays out the 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;
    }
  }
}

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

月下客 2024-07-17 19:43:58

System.Windows.Forms.FlowLayoutPanel 可以完成这项工作。

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

System.Windows.Forms.FlowLayoutPanel can do the job.

Just put the ToolStrip controls in it with correct order.

掩饰不了的爱 2024-07-17 19:43:58

我认为在您的情况下,您只需将 ToolStrip 上的 LayoutStyle 设置为 ToolStripLayoutStyle.Horizo​​ntalStackWithOverflow 即可摆脱困境,而不需要自己的自定义 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.

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