ToolStripPanel Join方法不是向面板添加工具条

时间:2014-10-10 22:44:29

标签: c# winforms toolstrip toolstrippanel

我对ToolStripPanel.Join有一个非常奇怪的问题,我一直在Google和SO上搜索一些关于我做错了什么的线索,但我无法弄明白。基本上,当我使用ToolStripPanel.Join时,我添加的第一个ToolStrip根本不会出现在ToolStripPanel中,但是我将加入我加入的所有其他ToolStrip。在我深入了解细节之前,让我首先说我正在使用C#和VS 2010以及.NET 4,并且仅仅针对某些上下文,我正在尝试在用户控件上使用ToolStripPanel。我们制作的自定义DLL,以便我们可以以其他形式重用这些用户控件。

我之前使用的是ToolStripContainer,但我决定转而使用ToolStripPanel,因为我们只需要ToolStripContainer的顶部面板;我没有看到使用ToolStripContainer的意义。由于我在工具箱中找不到ToolStripPanel控件,我决定将自己添加到Designer.cs文件中。我是这样做的:

private ToolStripPanel tsPanel;
<--Other code here-->
private void InitializeComponent()
{
    this.tsPanel = new System.Windows.Forms.ToolStripPanel();
    <--Other code here-->
    // 
    // tsPanel
    // 
    this.tsPanel.Dock = System.Windows.Forms.DockStyle.Top;
    this.tsPanel.Location = new System.Drawing.Point(0, 0);
    this.tsPanel.Margin = new System.Windows.Forms.Padding(3);
    this.tsPanel.Name = "tsPanel";
    this.tsPanel.Orientation = System.Windows.Forms.Orientation.Horizontal;
    this.tsPanel.RowMargin = new System.Windows.Forms.Padding(3, 0, 0, 0);
    this.tsPanel.Size = new System.Drawing.Size(1000, 0);
    <--Other code here-->
    // 
    // MFDesigner
    // 
    this.BackColor = System.Drawing.Color.Gainsboro;
    <--Add other controls to UC Controls collection-->
    this.Controls.Add(this.tsPanel);
    this.ForeColor = System.Drawing.Color.Black;
    this.Name = "MFDesigner";
    this.Size = new System.Drawing.Size(1000, 670);
    this.Load += new System.EventHandler(this.MultiFormatDesignerControl_Load);
    this.Resize += new System.EventHandler(this.MFDesigner_Resize);
    this.pnlToolbox.ResumeLayout(false);
    this.pnlProperties.ResumeLayout(false);
    this.ResumeLayout(false);
    this.PerformLayout();
}

然后,在用户控件的构造函数中,我有:

public MFDesigner()
{
    InitializeComponent();
    <--Other code here-->
    ToolStripButton[] openSaveButtonArr = new ToolStripButton[]{
        //The createToolStripButton method creates toolstrip buttons using some simple
        //parameters.
        createToolStripButton("Open", Images.CmdOpen, new EventHandler(this.OnOpen), "Open saved file"),
        createToolStripButton("Save", Images.CmdSave, new EventHandler(this.OnSaveToDisk), "Save to disk")
    };
    ToolStrip openSaveToolStrip = new ToolStrip(openSaveButtonArr);
    tspanel.Join(openSaveToolStrip);
    <--Other code here-->
}

由于我们正在创建工具条并将其添加到代码中的toolstrippanel,因此我无法在设计器中看到用户控件的外观。所以,我构建了dll,然后我在另一个项目中转到另一个表单,该项目使用dll中的用户控件,当表单打开时,没有工具条;它根本就没有出现。不过,这是奇怪的事情。如果我只向面板添加一个工具条,则会出现第二个工具条:

public MFDesigner()
{
    InitializeComponent();
    <--Other code here-->
    ToolStripButton[] openSaveButtonArr = new ToolStripButton[]{
        //The createToolStripButton method creates toolstrip buttons using some simple
        //parameters.
        createToolStripButton("Open", Images.CmdOpen, new EventHandler(this.OnOpen), "Open saved file"),
        createToolStripButton("Save", Images.CmdSave, new EventHandler(this.OnSaveToDisk), "Save to disk")
    };
    ToolStrip openSaveToolStrip = new ToolStrip(openSaveButtonArr);
    tspanel.Join(openSaveToolStrip, 1);
    ToolStripButton[] openSaveButtonArr2 = new ToolStripButton[]{
        createToolStripButton("Open2", Images.CmdOpen, new EventHandler(this.OnOpen), "Open saved rpx file 2"),
        createToolStripButton("Save2", Images.CmdSave, new EventHandler(this.OnSaveToDisk), "Save to disk 2")
    };
    ToolStrip openSaveToolStrip2 = new ToolStrip(openSaveButtonArr2);
    tsPanel.Join(openSaveToolStrip2, 1);
    <--Other code here-->
}

在上面的代码中,我创建的第一个工具条将不会出现,但第二个工具条(openSaveToolStrip2)将会出现。顺便说一句,如果我只使用两个工具条的Join过载(toolStrip),则不会出现任何内容。此外,如果我将工具条添加到其他行,即tsPanel.Join(toolstrip3,2)或tsPanel.Join(toolstrip4,3),将出现工具条。

似乎,对于一些莫名其妙的(至少对我而言)原因,我添加的第一个工具条从未出现,但所有后续工具条都会出现。作为一种解决方法,我一直在创建一个虚拟工具条,添加它,然后添加我所有的真实工具条。这感觉非常hacky所以我想知道为什么会发生这种情况。我试图按照文档on MSDN进行操作,但我仍然遗漏了一些东西,因为我无法想象这样的错误无法修复。

有人知道这里可能出现的问题吗?

1 个答案:

答案 0 :(得分:0)

使用工具条面板内的所有工具条运行应用程序的屏幕截图

Running application with all tool strips inside the tool strip panel

我接受了你的代码并删除了流浪成员,以便我可以编译,或者与你的问题无关的东西(例如: // private Panel pnlToolbox; // private Panel pnlProperties ; )。我创建了运行时工具条并将它们连接到面板,与您一样。我还提供了自己的 createToolStripButton 实现,因为你没有。第三个工具条使用 Image.FromStream 和Web资源,因为我缺少本地工具。无论有没有图像,我都没有任何故障......

以下完整示例:

public partial class MFDesigner : Form {
    private ToolStripPanel tsPanel;
    //private Panel pnlToolbox;
    //private Panel pnlProperties;        

    public MFDesigner ( )
    {
      InitializeComponent();
      //
      // first toolstrip
      //
      ToolStripButton tsbOpen = new ToolStripButton("Open", null, new EventHandler(this.OnOpen), "Open saved file");
      ToolStripButton tsbSave = new ToolStripButton("Save", null, new EventHandler(this.OnSaveToDisk), "Save to disk");
      ToolStripButton[] openSaveButtonArr = new ToolStripButton[] { tsbOpen, tsbSave };
      ToolStrip openSaveToolStrip = new ToolStrip(openSaveButtonArr) { CanOverflow = true };
      this.tsPanel.Join(openSaveToolStrip);    
      //
      // second toolstrip
      //
      ToolStripButton tsbOpen2 = createToolStripButton("Open2", null, new EventHandler(this.OnOpen), "Open saved file");
      ToolStripButton tsbSave2 = createToolStripButton("Save2", null, new EventHandler(this.OnSaveToDisk), "Save to disk");
      ToolStripButton[] openSaveButtonArr2 = new ToolStripButton[] { tsbOpen2, tsbSave2 };
      ToolStrip openSaveToolStrip2 = new ToolStrip(openSaveButtonArr2) { CanOverflow = true };
      this.tsPanel.Join(openSaveToolStrip2, 1);      
      //
      // third toolstrip
      //
      WebClient wc = new WebClient();
      byte[] bytes = wc.DownloadData("http://my.crestron.eu/images/icons/ico_folder_open.png");
      // You must keep the stream open for the lifetime of the Image.
      MemoryStream msOpen = new MemoryStream(bytes);
      System.Drawing.Image imgOpen = System.Drawing.Image.FromStream(msOpen);

      bytes = wc.DownloadData("http://www.njrussians.com/images/save_ico.png");
      MemoryStream msSave = new MemoryStream(bytes);
      System.Drawing.Image imgSave = System.Drawing.Image.FromStream(msSave);
      ToolStripButton tsbOpen3 = createToolStripButton("Open3", imgOpen, new EventHandler(this.OnOpen), "Open saved file");
      ToolStripButton tsbSave3 = createToolStripButton("Save3", imgSave, new EventHandler(this.OnSaveToDisk), "Save to disk");
      ToolStripButton[] openSaveButtonArr3 = new ToolStripButton[] { tsbOpen3, tsbSave3 };
      ToolStrip openSaveToolStrip3 = new ToolStrip(openSaveButtonArr3) { CanOverflow = true };
      this.tsPanel.Join(openSaveToolStrip3, 2);     

    }

    ToolStripButton createToolStripButton (string text, Image i, EventHandler eh, string name)
    {
      return new ToolStripButton(text, i, eh, name);
    }

    void MFDesigner_Resize (object sender, System.EventArgs e) { }

    void MFDesigner_Load (object sender, System.EventArgs e) { }

    void OnOpen (object target, EventArgs e) { }

    void OnSaveToDisk (object target, EventArgs e) { }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose (bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent ( )
    {
      this.tsPanel = new System.Windows.Forms.ToolStripPanel();
      this.SuspendLayout();
      // 
      // tsPanel
      // 
      this.tsPanel.Dock = System.Windows.Forms.DockStyle.Top;
      this.tsPanel.Location = new System.Drawing.Point(0, 0);
      this.tsPanel.Margin = new System.Windows.Forms.Padding(3);
      this.tsPanel.Name = "tsPanel";
      this.tsPanel.Orientation = System.Windows.Forms.Orientation.Horizontal;
      this.tsPanel.RowMargin = new System.Windows.Forms.Padding(3, 0, 0, 0);
      this.tsPanel.Size = new System.Drawing.Size(984, 0);
      // 
      // MFDesigner
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.BackColor = System.Drawing.Color.Gainsboro;
      this.ClientSize = new System.Drawing.Size(984, 632);
      this.Controls.Add(this.tsPanel);
      this.ForeColor = System.Drawing.Color.Black;
      this.Name = "MFDesigner";
      this.Text = "MFDesigner";
      this.Load += new System.EventHandler(this.MFDesigner_Load);
      this.Resize += new System.EventHandler(this.MFDesigner_Resize);
      this.ResumeLayout(false);
      this.PerformLayout();    
    }    


    #endregion    
  }