2014-10-10 39 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