尝试将ToolStrip与现有ToolStrip并排添加到ToolStripPanel [英] Trying to add a ToolStrip to a ToolStripPanel side-by-side with an existing ToolStrip

查看:160
本文介绍了尝试将ToolStrip与现有ToolStrip并排添加到ToolStripPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

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

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.

出于全面披露的目的,我应该警告您,我已将ToolStripPanel和其中一个工具条添加到基类窗体中,并且我正在尝试添加其他工具条继承自基类形式的子类形式。基类形式的ToolStripPanel和ToolStrip都被声明为受保护的朋友,因此应该可以使用。如前所述,子类表单的设计器窗口将允许我执行此操作(至少一次)。

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.

推荐答案

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

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

然后自定义LayoutEngine进行布局

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

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

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.

(它基于 MSDN LayoutEngine示例,已针对ToolStripPanels进行了修复)

hope that helps (it's based on MSDN LayoutEngine Example, fixed for ToolStripPanels)

Wyzfen

这篇关于尝试将ToolStrip与现有ToolStrip并排添加到ToolStripPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆