【发布时间】:2012-06-11 18:52:14
【问题描述】:
我的应用程序的每个工作区控件都有一个共同的ContextMenuStrip。
这个ContextMenuStrip 包含 4 个项目(“前移”、“后移”和“删除控件”)。
现在我想为一个控件扩展它。
此控件上有一个DataGridView,我想要一个附加项来删除选定的DataGridViewRow。
这是我试过的代码:
private void extendContextMenuOfDataGridViewRow (DataGridViewRow row) {
ContextMenuStrip ctx = new ContextMenuStrip();
foreach (ToolStripMenuItem item in this.ContextMenuStrip.Items) {
ctx.Items.Add(item);
}
ctx.Items.Add(new ToolStripSeparator());
ToolStripMenuItem ctxDeleteRow = new ToolStripMenuItem("Delete row");
ctxDeleteRow.Name = "ctxDeleteRow";
ctxDeleteRow.Click += new EventHandler(ctxDeleteRow_Click);
ctx.Items.Add(ctxDeleteRow);
row.ContextMenuStrip = ctx;
}
在foreach 循环的第一项添加到ctx.Items 后,调试器将离开整个方法,而公共ContextMenuStrip 中缺少第一项。
我该怎么做?
【问题讨论】:
-
“前移”、“后移”和“删除控件”= 3 个项目,而不是 4 个。
-
您到底想达到什么目的?您想在事件发生时将 MenuItem 添加到 ContextMenuStrip?
-
我将 [separator] 作为第四项(但类似于 html-tags)。我认为这就是它消失的原因。
标签: c# contextmenustrip