0

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?

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
pinki
  • 902
  • 2
  • 12
  • 29

2 Answers2

0

If you want to extend functionally of some control, you can either

a) create an extension method

public void DoSomething(this MyExtendedControl mec, DataGridViewRow row)
{
}

b) create a new class inheriting from your unsatisfactory control (or even create a completely new control), when you can override/add things as needed

Depends on your specific needs, couldn't understand from your description...

walther
  • 13,466
  • 5
  • 41
  • 67
0

I haven't worked with WinForms for ages, but are you sure that you can keep the same Row object assigned to two different Strips at once?

foreach (ToolStripMenuItem item in this.ContextMenuStrip.Items) {
    ctx.Items.Add(item);
}

I seriously doubt this should work by design because a row has to “talk” to its parent, and by adding it to another strip I'm afraid you're re-assigning the parent.

Instead, I would have added an item to the common menu but with its Visible property to false.
Then I would catch the menu opening event and make item visible if target is a DataGridViewRow.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • I want to set the extended `ContextMenuStrip` to the parameter's `Row` object. This `Row` object does not have any `ContextMenuStrip` at this time. The `ContextMenuStrip` I want to extend is from the control which contains the `DataGridView`. – pinki Jun 11 '12 at 11:08
  • What I see is you're taking an *existing* `ContextMenuStrip` and creating a *new one* (`ctx`). There is no such thing as “extending” an object in C#. And my point is, if you `Add` items to `ctx`, they will likely disappear from the original one. So you'd be better off having **the same context menu strip** assigned *both* to rows and other controls, and showing/hiding “Delete Row” item depending on the context. – Dan Abramov Jun 11 '12 at 12:17