/// <summary> /// Adds a new ToolStripItem /// </summary> /// <param name="toolList">List of Tools of the Toolbar</param> /// <param name="toolId">Id of the tool</param> /// <param name="style">Type of ToolStripItem</param> /// <param name="index">Index to insert</param> /// <returns>A new ToolStrip Item</returns> private static ToolStripItem Add(ToolsList toolList, string toolId = "", ToolbarHelper.ConstantsType style = ToolbarHelper.ConstantsType.ssTypeButton, int index = 0) { //First we check if we are inserting in the Tools of the Toolbar or in the Tools of a ToolStripItem if (InsertionInToolsOfToolStripItem(toolList)) { //Let's create the real ToolStripItem which is the one that will be displayed in the Form var tuple = AddToolStripItem(toolList, toolId, index); if (tuple != null) { //If index is greater that List Count assign back the count to the index to keep consistency if (index > toolList.Count) { index = toolList.Count; } //Now the ToolStripItem is included as part of the tools of the ToolStripItem toolList.Insert(index, tuple.Item2); //Return the ToolStripItem created return(tuple.Item1); } } else { //If the code jumps here means we are inserting ToolStripItems in the Tools of the Toolbar //Create the wrapper for ToolStripItem ToolStripItemWrapper itemWrapper = new ToolStripItemWrapper(); itemWrapper.ToolbarParent = toolList.ToolbarParent; //Assign its Toolbar parent itemWrapper.Name = toolId; //Assign its id itemWrapper.Text = SetText(itemWrapper.Text, toolId); //Assign its text itemWrapper.ConstantsType = style; //Assign the type //If the Type of the ToolStripItem will be Combobox, then create the combobox instance for its handling if (style == ToolbarHelper.ConstantsType.ssTypeComboBox) { itemWrapper.ComboBox = new ComboBoxWrapper(itemWrapper.ToolbarParent, itemWrapper.Name); } //If the Type of the ToolStripItem will be Menu, then create the menu instance for its handling else if (style == ToolbarHelper.ConstantsType.ssTypeMenu) { itemWrapper.Menu = new ToolStripItemWrapperMenu(toolId, toolList.ToolbarParent); //It's important to mention this: When a ToolStripItem of type Menu is created it can be work as a popup menu into any other control. //To support this we add a new ToolStripDown and we include the id of the tool in the list of popup menus toolList.ToolbarParent.PopupMenus.Add(toolId, CreateToolStripDown(itemWrapper, toolId)); } //Finally the new ToolStripItem is added to the list of tools of the toolbar toolList.Add(itemWrapper); //Return the ToolStripItem created return(itemWrapper); } return(null); }
/// <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); }
/// <summary> /// Determines whether the type/style of a ToolStripItem is changed or not /// </summary> /// <param name="style">ToolStripItem style</param> /// <param name="toolID">ID of the Tool</param> /// <returns>The new ToolStripItem style</returns> private static ToolbarHelper.ConstantsType ChangeTypeIfNeeded(ToolbarHelper.ConstantsType style, string toolID) { if (toolID.Equals(SEPARATOR)) { return(ToolbarHelper.ConstantsType.ssTypeSeparator); } return(style); }
/// <summary> /// Adds a new ToolStripItem /// </summary> /// <param name="tools">List of ToolStrip Items</param> /// <param name="toolId">Id of the tool. Empty by default</param> /// <param name="style">Type of ToolStrip Item . Button by default</param> /// <param name="index">Index to insert. 0 by default </param> /// <returns>A new ToolStrip Item</returns> public static ToolStripItem Add(this ITools tools, string toolId = "", ToolbarHelper.ConstantsType style = ToolbarHelper.ConstantsType.ssTypeButton, int index = 0) { if (tools is ToolsList) // If the list belongs to the tools of the toolbar { return(Add(tools as ToolsList, toolId, style, index)); } else // If the list belongs to the menu tools of a ToolStripItem { return(Add(tools as ToolsListMenu, toolId, style, index)); } }
/// <summary> /// Creates a new instance of ToolStripItem /// </summary> /// <param name="type">ToolStripItem type</param> /// <param name="wrapper">ToolStripItem wrapped</param> /// <returns>A new instance of ToolStripItem</returns> private static ToolStripItem CreateToolStripItem(ToolbarHelper.ConstantsType type, ToolStripItemWrapper wrapper) { ToolStripItem item = null; switch (type) { case ToolbarHelper.ConstantsType.ssTypeButton: item = new ToolStripButton(); break; case ToolbarHelper.ConstantsType.ssTypeStateButton: item = new ToolStripButton(); break; case ToolbarHelper.ConstantsType.ssTypeMenu: item = new ToolStripDropDownButton(); break; case ToolbarHelper.ConstantsType.ssTypeComboBox: item = new ToolStripComboBox(); break; case ToolbarHelper.ConstantsType.ssTypeSeparator: item = new ToolStripSeparator(); break; default: item = new ToolStripButton(); break; } if (wrapper != null) { //The properties of the wrapped object are cloned in the real ToolStripItem item = CloneProperties(item, wrapper); } return(item); }