Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join context menus

If I have a context menu, is it possible to join it to another menu? So you get:

  Menu 1 Item 1
  Menu 1 Item 2
  Menu 1 Item N
    ---------
  Menu 2 Item 1
  Menu 2 Item 2
  Menu 2 Item N

Let's take an example of a notepad-like program. There is repetition in the menus in that there is a standard set of tools that appear in both the edit menu and the edit controls context menu (the Cut, Copy, Paste, Select All...).

I'd like to have a menu called ClipboardTools, which will appear in both the Edit and control context menus, without needing to create the items more than once. Of course in this case the repetition isn't that bad, but I have to deal with larger menus that appear in 3-4 different menus, and ideally not as sub menus.

like image 826
Matt Avatar asked Feb 24 '12 14:02

Matt


1 Answers

Yes. Since each menuItem is a separate control, you can add the same menuItem to both menu;

  var joinedMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  menu1.Items.Add(joinedMenuItem );
  menu2.Items.Add(joinedMenuItem );

The same way you may add items from one menu to other menu;

  menu1.Items.AddRange(menu2.Items);

That is time to say that:
ToolStripMenu sub-items are it Items property.
ToolStripMenuItem sub-items are in DropDownItems property.

So if you have two top menu-items (like File and Edit) and you want to remove shared subitems:

  foreach (var item in topMenuItem2.DropDownItems)
  {
      topMenuItem1.DropDownItems.Remove(item);
  }

In the real life, if one want such flexible menu system, most probable one would create total list of menu items. And maybe subsets of menu Items by categories. Then one would add menu-items from such list or sets to the displayed menu.

As example, you can provide possibility for users to customize the menu or create their own custom menu sections. As it is done in Visual Studio.

like image 105
MajesticRa Avatar answered Nov 02 '22 04:11

MajesticRa