2014-10-10 52 views
0

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

我以前使用的是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 overload Join(toolStrip),则不会显示任何内容。另外,如果我将工具条添加到其他行,即tsPanel.Join(toolstrip3,2)或tsPanel.Join(toolstrip4,3),将出现工具条。

对于一些令人费解的(至少对我来说)理由来说,我添加的第一个工具条永远不会出现,但所有后续的工具条都会这样做。作为一种解决方法,我一直在创建一个虚拟工具条,添加它,然后添加我所有的真实工具条。这感觉很不方便,所以我很想弄清楚为什么会发生这种情况。我试图按照文档on MSDN,但我仍然必须失去一些东西,因为我无法想象这样的错误没有得到修复。

有没有人知道这里可能会出错?与工具条面板中的所有工具条

Running application with all tool strips inside the tool strip panel

我把你的代码,并删除杂散成员,这样我可以编译或东西,是不相关的您的问题正在运行的应用的

+0

不知道这是相关的或没有,但以防万一...这个项目是从2008年VS升级和.NET 3.5 VS 2010和.NET 4 – greyseal96 2014-10-13 23:50:50

回答

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  
    } 
+0

我感谢你抽出时间把这一切嘲笑起来。不幸的是,这不是很有帮助,因为它基本上证实了一些奇怪的事情的确在发生,但它并没有给出任何可能导致问题的线索。 – greyseal96 2014-10-13 18:35:10

+0

我刚刚注意到您正在使用表单进行测试。不知道我是否在上面说过,但是我把所有这些放在一个dll项目的用户控件中,然后我将这个用户控件放到另一个项目的窗体中。你认为这有什么区别? – greyseal96 2014-10-14 23:36:07

+0

>“你甚至不确定是否有细节......你甚至懒得去读你原来的帖子。” 不知道为什么你觉得有必要在你的评论结尾处加上那个粗暴无礼的部分。你可以简单地解决我在一个dll中使用用户控件并在另一个项目中使用它的问题。 顺便说一句,我认为这听起来可能听起来更好,说:“不知道我是否说过它或不......”,而不是,“嘿,不知道你是否阅读我原来的文章,因为你使用的表单,它不不会重现我的原始状况,而且你也没有解释为什么你选择这样做。“ – greyseal96 2014-10-16 01:04:21