Skip to content

ContextMenuStrips aren't dynamic?

d g

Asked Jun 11, 2006, 7:29 AM EDT1 answer
Share
LinkedInX


My question is concerning the difference between contextMenuStrips and the old contextMenus.

contextMenus are easily dynamic, for instance:

contextMenu menu1 = new contextMenu();
menu1.MenuItems.Add("first");
menu1.MenuItems.Add("second");
menu1.MenuItems[1].MenuItems.Add("submenu of second");
... etc

however, I can't seem to find the same flexibility in the contextMenuStrips... MSDN ignorantly states that their example is dynamic...
(http://msdn2.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx)

but it hardcodes names and structure... thats not dynamic creation... their example is only an example of what is done when you visually create the contextMenuStrip at design time... I want to build the contextMenuStrip menus not knowing at design time anything at all about the structure or names of the menu items...

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    okay, i guess I'm going to answer my own question... it suddenly clicked...

    ContextMenuStrip menu1 = new ContextMenuStrip();
    ToolStripMenuItem []items = new ToolStripMenuItem[100];

    menu1.Items.Add("first");
    items[0] = new ToolStripMenuItem();
    menu1.Items.AddRange(new ToolStripItem[] { items[0] });
    menu1.Items.Add("third");

    items[0].Text = "second";
    items[0].DropDownItems.Add("submenu of second");

    items[1] = new ToolStripMenuItem();
    items[0].DropDownItems.AddRange(new ToolStripItem[] { items[1] });

    items[1].Text = "going deeper still...";
    items[1].DropDownItems.Add("submenu of submenu of second");

    ... the .net 2.0 implementation is certainly not easier >=|   ... however, it seems its added functionality makes it worth it

    0