菜单动态合并:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Ex01_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.MdiParent = this;
f.Show();//显示子窗体
f.Resize += new EventHandler(f_Resize);
}
void f_Resize(object sender, EventArgs e)
{
Form2 f = (Form2)sender;
ToolStripMenuItem item = new ToolStripMenuItem();
for (int i = 0; i < f.contextMenuStrip2.Items.Count; )//合并菜单
{
item.DropDownItems.Add(f.contextMenuStrip2.Items[i]);
}
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { item });
}
}
}
问题:
1.把子窗体的contextMenuStrip2设置成公有的,即:public System.Windows.Forms.ContextMenuStrip contextMenuStrip2;
2.把父窗体的Form1的contextMenuStrip属性设置成contextMenuStrip1。