用于多个 ContextMenuStrip 的 ToolStripMenuItem
我有一个表单,其中包含带有许多点击页面的选项卡面板。它们每个都有自己的上下文菜单(右键单击时显示)。但是,如果我将 ToolStripMenuItem 添加到多个 ContextMenuStrip,则只有最后一个菜单条真正具有此菜单项。 简单的代码示例是:
ToolStripMenuItem tim_refresh = new ToolStripMenuItem("Refresh", null, (o, e) =>
{
MessageBox.Show("Refresh");
});
ContextMenuStrip cms1 = new ContextMenuStrip();
cms1.Items.Add(tim_refresh);
ContextMenuStrip cms2 = new ContextMenuStrip();
cms2.Items.Add(tim_refresh);
this.tblDataManagerObjects.ContextMenuStrip = cms1;
this.tblDataSourceTypes.ContextMenuStrip = cms2;
如果一个一个地显示这个菜单,第一个将是空的......我怎样才能实现我想要的?
I have a form which contains tab panel with many tap pages. Each of them has its own context menu (display on right-click). But If I add a ToolStripMenuItem to multiple ContextMenuStrips only last menu strip really has this menu item.
Simple code example is:
ToolStripMenuItem tim_refresh = new ToolStripMenuItem("Refresh", null, (o, e) =>
{
MessageBox.Show("Refresh");
});
ContextMenuStrip cms1 = new ContextMenuStrip();
cms1.Items.Add(tim_refresh);
ContextMenuStrip cms2 = new ContextMenuStrip();
cms2.Items.Add(tim_refresh);
this.tblDataManagerObjects.ContextMenuStrip = cms1;
this.tblDataSourceTypes.ContextMenuStrip = cms2;
If one shows this menus one by one, first will be empty...How can I achieve what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为视觉对象不能同时是多个不同视觉对象的子对象。在您的情况下,
tim_refresh
同时是cms1
和cms2
的子项。您需要创建两个完全独立的
ToolStripMenuItem
实例。编辑:
您可以在因子方法中提取视觉创建以简化多个对象实例化:
Thi is because visual can not be child of multiple diferent visuals in the same time. In your case
tim_refresh
is a child ofcms1
andcms2
at the same time.You need to create two completely separate instances of
ToolStripMenuItem
.EDIT:
You can extract visual creation in factor method to simplify multiple objects instantiation:
一次显示一个上下文菜单;您可能不需要到处都有许多克隆,但您可能希望在菜单条打开时将菜单项的一个实例移动到菜单菜单条;
当本地副本为空时,我将此处指定的(祖父)父级项目移动到子级(当前打开的)菜单,并且所有下一个 ctx 开口我只是 AddRange,这会从先前打开的菜单项中移动“定位”的三个菜单项ctxMenuStrip 到当前打开的菜单:
one context menu is displayed once at a time; you probably don't need many clones everywhere, but you may want to move one single instance of your menu items to the menu menu strip at the moment menu strip is opening;
I'm moving here the named (grand) parent's items to the child (currently opening) menu when the local copy is empty, and all the next ctx openings I just AddRange, which moves the "located" three menu items from the previously opened ctxMenuStrip to the currently-opening-one: