我有一个容器,里面有一个控件。我有一个分配给这两个的 ContextMenuStrip。我想要发生的是控件的上下文菜单也包含容器中的所有项目。但是,当我尝试
ToolStripManager.Merge(this.ContextMenuStrip, this.Parent.ContextMenuStrip);
整个容器的上下文菜单被删除
编辑:好的,我想我需要澄清一下。我的代码在某种程度上有效。this.ContextMenuStrip 是我为其分配了一个菜单的控件的属性,而 this.Parent.ContextMenuStrip 我已分配了一个不同的菜单。当我合并它们时,父菜单的所有项目都显示在子菜单中,但父菜单不再有菜单。
这就是我设置菜单的方式:
this.ContextMenuStrip = contextmenu1;
this.Parent.ContextMenuStrip = contextmenu2;
好的,这是代码
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);
}
}
}