I've a common ContextMenuStrip
for every workspace control of my application.
This ContextMenuStrip
contains 4 Items ("Move front", "Move back", and "Delete control").
Now I want to extend it for one control.
There's a DataGridView
on this control and I want an additional item to delete the selected DataGridViewRow
.
This is the code I tried:
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;
}
After the first item of the foreach
loop was added to ctx.Items
the debugger leaves the whole method and the first item is missing at the common ContextMenuStrip
.
How do I do that right?