/// <summary> /// Adds a ToolStripItem to the Items of a Toolbar /// </summary> /// <param name="toolList">Tools List</param> /// <param name="toolId">Tool ID to insert</param> /// <param name="index">Index to Insert</param> /// <returns>A tuple that contains the instance of the new ToolStripItem and the instance of ToolStripItem wrapped</returns> private static Tuple <ToolStripItem, ToolStripItemWrapper> AddToolStripItem(ToolsList toolList, string toolId, int index) { //In VB6 sometimes the tool is gotten with symbol "&" so we had to try twice (just in case) var toolInTools = toolList.ToolbarParent.ToolbarRoot.Tools[toolId]; if (toolInTools == null) { toolInTools = toolList.ToolbarParent.ToolbarRoot.Tools["&" + toolId]; } if (toolInTools != null) { //Let's check if the type/style of the ToolStripItem should be modified toolInTools.ConstantsType = ChangeTypeIfNeeded(toolInTools.ConstantsType, toolId); //Create the real ToolStripItem (the one that will be displayed in the Form) ToolStripItem item = CreateToolStripItem(toolInTools.ConstantsType, toolInTools); if (index > (toolList.ToolbarParent as ToolStrip).Items.Count) { index = (toolList.ToolbarParent as ToolStrip).Items.Count; } //Add the real ToolStripItem to the items of the Toolbar (toolList.ToolbarParent as ToolStrip).Items.Insert(index, item); //Finally return the tuple with both instances return(new Tuple <ToolStripItem, ToolStripItemWrapper>(item, toolInTools)); } return(null); }
/// <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); }
public ToolStripWrapper() { Tools = new ToolsList(this); Toolbars = new ToolbarsList(this); PopupMenus = new Dictionary <string, ToolStripDropDown>(); }
/// <summary> /// Checks if the Insertion is in the tools of the toolbar or in the tools of a ToolStripItem /// </summary> /// <param name="toolList"></param> /// <returns></returns> private static bool InsertionInToolsOfToolStripItem(ToolsList toolList) { //If ToolbarRoot is null means the insertion is in the Tools of a ToolStripItem, not in the Tools of a Toolbar return(toolList.ToolbarParent.ToolbarRoot != null); }
/// <summary> /// Remove a ToolStripItem from all its instances /// </summary> /// <param name="toolList">List of ToolStripItem</param> /// <param name="toolId">Key to remove the ToolStripItem</param> public static void Remove(this ToolsList toolList, string toolId) { bool popupMenuAlreadyChecked = false; //The ToolStripItem is seeked in all toolbars foreach (IToolbar toolbar in toolList.ToolbarParent.Toolbars) { if (!String.IsNullOrWhiteSpace(toolList[toolId].MenuId)) { var toolMenu = toolbar.Tools[toolList[toolId].MenuId]; if (toolMenu != null) { var menu = toolMenu.Menu; if (menu != null) { menu.Tools.Remove(toolList[toolId]); } } } //Is removed from the tools of the toolbar toolbar.Tools.Remove(toolList[toolId]); //Is removed from the items of the toolbar (ToolStrip .NET) (toolbar as ToolStrip).Items.RemoveByKey(toolId); //Now let's check if should be removed from the popup menus etc if (toolbar.ToolbarRoot != null && !popupMenuAlreadyChecked) { //The popup menus are gotten var popupMenus = toolbar.ToolbarRoot.PopupMenus; if (popupMenus.Count > 0) { //If is in the popup menus list is removed if (popupMenus.ContainsKey(toolId)) { popupMenus.Remove(toolId); } //The popup menus of the control are iterated foreach (var popupMenu in popupMenus) { if (popupMenu.Value != null) { //The ToolStripItem is removed from the Items of the ToolStripDropDown popupMenu.Value.Items.RemoveByKey(toolId); //Finally let's check if the ToolStripItem was included as DropDownItem, if so must be removed as well var menu = ((toolbar as ToolStrip).Items[popupMenu.Key.ToString()] as ToolStripDropDownButton); if (menu != null && menu.DropDownItems.ContainsKey(toolId)) { menu.DropDownItems.RemoveByKey(toolId); popupMenuAlreadyChecked = true; } } } } } } toolList.Remove(toolList[toolId]); }