我對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,但我仍然必須失去一些東西,因爲我無法想象這樣的錯誤沒有得到修復。
有沒有人知道這裏可能會出錯?與工具條面板中的所有工具條
我把你的代碼,並刪除雜散成員,這樣我可以編譯或東西,是不相關的您的問題正在運行的應用的
不知道這是相關的或沒有,但以防萬一...這個項目是從2008年VS升級和.NET 3.5 VS 2010和.NET 4 – greyseal96 2014-10-13 23:50:50