2013-04-07 153 views
0

我有一個內部控件的容器。我有一個ContextMenuStrip分配給這兩個。 我想要發生的是控件的上下文菜單也包含容器中的所有項目。 然而,當我嘗試兒童ContextMenuStrip

ToolStripManager.Merge(this.ContextMenuStrip, this.Parent.ContextMenuStrip); 

整個容器中的文本菜單被刪除

編輯:好的,我想我需要澄清。 我的代碼在某種程度上有效。 this.ContextMenuStrip是我爲其分配了一個菜單的控件的屬性,並且this.Parent.ContextMenuStrip我分配了不同的菜單。當我合併它們時,父級菜單的所有項目都顯示在子菜單中,但父級不再有菜單。

這是我的設置菜單:

this.ContextMenuStrip = contextmenu1; 
this.Parent.ContextMenuStrip = contextmenu2; 

OK,這裏的代碼

在Form1.designer.cs:

namespace FailExample 
{ 
    partial class Form1 
    { 
     /// <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); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.components = new System.ComponentModel.Container(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(this.components); 
      this.item2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); 
      this.item1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.contextMenuStrip2.SuspendLayout(); 
      this.contextMenuStrip1.SuspendLayout(); 
      this.SuspendLayout(); 
      // 
      // button1 
      // 
      this.button1.ContextMenuStrip = this.contextMenuStrip2; 
      this.button1.Location = new System.Drawing.Point(86, 82); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(75, 23); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "button1"; 
      this.button1.UseVisualStyleBackColor = true; 
      // 
      // contextMenuStrip2 
      // 
      this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.item2ToolStripMenuItem}); 
      this.contextMenuStrip2.Name = "contextMenuStrip2"; 
      this.contextMenuStrip2.Size = new System.Drawing.Size(105, 26); 
      // 
      // item2ToolStripMenuItem 
      // 
      this.item2ToolStripMenuItem.Name = "item2ToolStripMenuItem"; 
      this.item2ToolStripMenuItem.Size = new System.Drawing.Size(104, 22); 
      this.item2ToolStripMenuItem.Text = "Item2"; 
      // 
      // contextMenuStrip1 
      // 
      this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.item1ToolStripMenuItem}); 
      this.contextMenuStrip1.Name = "contextMenuStrip1"; 
      this.contextMenuStrip1.Size = new System.Drawing.Size(105, 26); 
      // 
      // item1ToolStripMenuItem 
      // 
      this.item1ToolStripMenuItem.Name = "item1ToolStripMenuItem"; 
      this.item1ToolStripMenuItem.Size = new System.Drawing.Size(104, 22); 
      this.item1ToolStripMenuItem.Text = "Item1"; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 261); 
      this.ContextMenuStrip = this.contextMenuStrip1; 
      this.Controls.Add(this.button1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.Load += new System.EventHandler(this.Form1_Load); 
      this.contextMenuStrip2.ResumeLayout(false); 
      this.contextMenuStrip1.ResumeLayout(false); 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.Button button1; 
     private System.Windows.Forms.ContextMenuStrip contextMenuStrip2; 
     private System.Windows.Forms.ToolStripMenuItem item2ToolStripMenuItem; 
     private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; 
     private System.Windows.Forms.ToolStripMenuItem item1ToolStripMenuItem; 
    } 
} 

而且Form1.cs的

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace FailExample 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      ToolStripManager.Merge(button1.Parent.ContextMenuStrip, button1.ContextMenuStrip); 
     } 
    } 
} 
+0

您是否有兩個單獨的上下文菜單?這從您的問題不可見。看起來你有一個'ContextMenuStrip'分配給兩者。 – 2013-04-07 06:08:23

+0

您忽略了Merge()的返回值。當AllowMerge屬性未設置爲true時,返回* false *。當然你的問題。 – 2013-04-07 13:26:22

+0

@ MD.Unicorn是的,我有兩個單獨的菜單。另外'ContextMenuStrip'是所有控件的一個屬性。我已經爲控件及其父項分配了不同的菜單。 – Jay 2013-04-07 14:32:24

回答

2

我得到一個NullReferenceExcep當我嘗試你的例子時。但這適用於我:

ToolStripManager.Merge(contextMenuStrip1, contextMenuStrip2); 

我認爲您對ContextMenuStrips的引用是錯誤或相同的。

當我看信息VS顯示在鼠標懸停時:

  • this.ContextMenuStrip指墊層形式
  • this.Parent.ContextMenuStrip指形式的父(這是空)
  • 無論如何都不參考資料是指您的上下文菜單ToolStripContainer
+1

您正在獲取NullReferenceException,因爲您尚未向其分配任何內容。你需要在這個地方執行'this.ContextMenuStrip = contextMenuStrip1;'和'this.Parent.ContextMenuStrip = contextMenuStrip2'。 – Jay 2013-04-07 14:38:27