1

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

我之前使用的是 ToolStripContainer,但我决定改用 ToolStripPanel,因为我们只需要 ToolStripContainer 的顶部面板;我没有看到使用 ToolStripContainer 的意义。由于在 Toolbox 中找不到 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-->
}

由于我们正在创建工具条并将它们添加到代码中的工具条面板中,因此我无法在用户控件的设计器中看到它的外观。因此,我构建了 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 重载 Join(toolStrip),则什么都不会出现。此外,如果我将工具条添加到其他行,即 tsPanel.Join(toolstrip3, 2) 或 tsPanel.Join(toolstrip4, 3),则会出现工具条。

似乎,出于某种莫名其妙(至少对我而言)的原因,我添加的第一个工具条从未出现,但所有后续工具条都会出现。作为一种解决方法,我一直在创建一个虚拟工具条,添加它,然后添加我所有的真实工具条。这感觉很hacky,所以我很想弄清楚为什么会这样。我试图按照MSDN 上的文档进行操作,但我仍然必须遗漏一些东西,因为我无法想象像这样的错误没有得到修复。

有人知道这里可能出了什么问题吗?

4

1 回答 1

0

正在运行的应用程序的屏幕截图,其中所有工具条都位于工具条面板中

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

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

完整示例如下:

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    
  }
于 2014-10-11T07:14:56.927 回答