/// <summary> /// Adds a new ToolStripItem of type Menu /// </summary> /// <param name="toolListMenu">List of Menu Tools</param> /// <param name="toolId">Id of the tool</param> /// <param name="style">Type of ToolStrip Item . Button by default</param> /// <param name="index">Index to insert.</param> /// <returns>A new ToolStrip Item</returns> private static ToolStripItem Add(ToolsListMenu toolListMenu, string toolId, ToolbarHelper.ConstantsType style, int index) { ToolStripMenuItem ToolStripMenuItem = null; //First we get the ToolStripDropDown located by ID in the list of Popup Menus var toolStripDropDown = toolListMenu.ToolbarParent.PopupMenus[toolListMenu.MenuId]; //Let's check if the type/style of the ToolStripItem should be modified style = ChangeTypeIfNeeded(style, toolId); //Second we get the ToolStripItem by ID from the tools of the Toolbar var toolInTools = toolListMenu.ToolbarParent.Tools[toolId]; //If the ToolStripItem does not exist means the Tool was not created, then we create a new one and continue the execution if (toolInTools == null && toolId != SEPARATOR) { //Add the new ToolStripItem to the tools of the Toolbar** toolListMenu.ToolbarParent.Tools.Add(toolId, style, index); //Now is inserted, let's try to get again the ToolStripItem by ID from the tools of the Toolbar toolInTools = toolListMenu.ToolbarParent.Tools[toolId]; } //Then we need to create the real ToolStripItem which is the one that will be displayed/painted in the Form. ToolStripItem item = CreateToolStripItem(style, toolInTools); //The new ToolStripItem is added to the ToolStripDropDown, in other words the submenu of the menu is created toolStripDropDown.Items.Add(item); //Then we need to insert the new ToolStripItem( the submenu ) in all menus of the toolbars ( just in case the menu is present in several toolbars at once). //Example, suppose you have 2 toolbars "toolbar1" and "toolbar2", also you have a ToolStripItem of type Menu "Menu1" which was inserted in both toolbars. //Let's say you are adding a submenu "Close" that belongs to the Menu "Menu1", since "Menu1" is present in two toolbars both collections must be modified foreach (IToolbar toolbar in toolListMenu.ToolbarParent.Toolbars) { //Get the ToolStripItem(Menu) from the items of the ToolStripItems var menu = (toolbar as ToolStrip).Items[toolListMenu.MenuId]; if (menu != null) { //The SubMenu is added to the Items of the Menu ToolStripMenuItem = new ToolStripMenuItem(); ToolStripMenuItem.Text = SetText(ToolStripMenuItem.Text, toolId);; ToolStripMenuItem.Name = toolId; (menu as ToolStripDropDownButton).DropDownItems.Add(ToolStripMenuItem); } } //If the ToolStripItem was not present initially we must set correctly the ToolbarParent** if (toolInTools != null) { toolInTools.ToolbarParent = toolListMenu.ToolbarParent; toolInTools.MenuId = toolListMenu.MenuId; //Finally we must add the submenu (ToolStripItemWrapped) to the tools of the menu toolListMenu.Add(toolInTools); } //Return the ToolStripItem created return(toolInTools); }
public ToolStripItemWrapperMenu(string toolId, IToolbar toolbarRoot) { Tools = new ToolsListMenu(toolId, toolbarRoot); }