public static ToolStripDropDownItem GetItem( this ToolStripItemCollection menu, string path, Action <ToolStripDropDownItem> onCreate = null, List <ToolStripDropDownItem> addList = null) { string[] parts = path.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); ToolStripItemCollection current = menu; for (int i = 0; i < parts.Length; i++) { bool isLast = i == parts.Length - 1; if (current.ContainsKey(parts[i])) { ToolStripItem item = current[parts[i]]; if (item is ToolStripDropDownItem mitem) { current = mitem.DropDownItems; } else { mitem = CreateItem(parts[i]); addList?.Add(mitem); onCreate?.Invoke(mitem); current.Add(mitem); current = mitem.DropDownItems; } if (isLast) { return(mitem); } } else { ToolStripDropDownItem item = CreateItem(parts[i]); addList?.Add(item); onCreate?.Invoke(item); current.Add(item); current = item.DropDownItems; if (isLast) { return(item); } } } return(null); }
private void InsertItem(IEnumerable <string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) { ToolStripDropDownItem parent = null; foreach (string s in structure) { if (parentItems.ContainsKey(s)) { parent = (ToolStripDropDownItem)parentItems[s]; } else { parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s);; parentItems.Add(parent); } parentItems = parent.DropDownItems; } parentItems.Add(item); }
private void AddToMenu(ToolStripItemCollection items, NodeToken token, string path, EventHandler click) { var pathParts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var first = pathParts.FirstOrDefault(); ToolStripMenuItem item = null; if (!items.ContainsKey(first)) { item = new ToolStripMenuItem(first); item.Name = first; item.Tag = token; items.Add(item); } else { item = items[first] as ToolStripMenuItem; } var next = string.Join("/", pathParts.Skip(1)); if (!string.IsNullOrEmpty(next)) { item.MouseEnter += (sender, args) => OnNodeHint(""); AddToMenu(item.DropDownItems, token, next, click); } else { item.Click += click; item.Click += (sender, args) => { var i = allContextItems.Keys.FirstOrDefault(x => x.Name == item.Name); allContextItems[i]++; }; item.MouseEnter += (sender, args) => OnNodeHint(token.Attribute.Description ?? ""); if (!allContextItems.Keys.Any(x => x.Name == item.Name)) { allContextItems.Add(item, 0); } } }
void AddMenu(List <string> menuItems) { ToolStripItemCollection itemLast = contextMenuStrip1.Items; while (menuItems.Count > 0) { string itemNow = menuItems[0]; menuItems.RemoveAt(0); ToolStripMenuItem item = null; if (itemLast.ContainsKey(itemNow)) { item = itemLast[itemNow] as ToolStripMenuItem; } else { item = new ToolStripMenuItem(itemNow); item.Name = itemNow; itemLast.Add(item); item.DropDownItemClicked += contextMenuStrip1_OnItemClicked; } itemLast = item.DropDownItems; } }
public static void PopulateToolStripMenuWithDBEntries(ToolStripItemCollection itemCollection, Type dbEntryType, ToolStripItemClickedEventHandler onClickEventHandler = null, bool addRandomOption = false, string tagPrefix = "") { if (addRandomOption) { itemCollection.Add("(Random)").Tag = ""; } foreach (string id in Database.Instance.GetAllEntriesIDs(dbEntryType)) { ToolStripItemCollection parentCollection = itemCollection; DBEntry dbEntry = Database.Instance.GetEntry(dbEntryType, id); if (dbEntry == null) { continue; } if (!string.IsNullOrEmpty(dbEntry.UICategory)) // Database entry belong to a collection { string categoryName = dbEntry.UICategory; string categoryID = "*" + dbEntry.UICategory; if (!parentCollection.ContainsKey(categoryID)) { ToolStripItem categoryItem = parentCollection.Add(categoryName); categoryItem.Name = categoryID; categoryItem.BackColor = BLUEPRINT_BACKCOLOR_MENU; categoryItem.Font = BLUEPRINT_FONT; categoryItem.ForeColor = BLUEPRINT_FORECOLOR; } parentCollection = ((ToolStripMenuItem)parentCollection[categoryID]).DropDownItems; } ToolStripMenuItem item = new ToolStripMenuItem { Text = dbEntry.UIDisplayName, ToolTipText = dbEntry.UIDescription, Tag = tagPrefix + id, BackColor = BLUEPRINT_BACKCOLOR_MENU, Font = BLUEPRINT_FONT, ForeColor = BLUEPRINT_FORECOLOR, }; parentCollection.Add(item); } foreach (ToolStripMenuItem item in itemCollection) { if (item.DropDownItems.Count > 0) { if (onClickEventHandler != null) { item.DropDownItemClicked += onClickEventHandler; } SortContextMenuStrip(item.DropDownItems); } } SortContextMenuStrip(itemCollection); }
private void ContextMenuOpening(object sender, EventArgs e) { MultiDiagramContextMenuStrip contextMenu = ContextMenuStrip; ToolStripItemCollection menuItems = contextMenu.Items; Diagram diagram = contextMenu.SelectedDiagram; bool haveSelectedTab = diagram != null; foreach (ToolStripItem item in menuItems) { if (item.Tag == ContextMenuItemNeedsSelectedTab) { item.Enabled = haveSelectedTab; } } Partition partition = this.DocData.Store.DefaultPartition; //Disable/Enable New Diagram Tabs stuff wow ToolStripDropDownItem newPageMenuItem = (ToolStripDropDownItem)menuItems[ResourceStrings.DiagramCommandNewPage]; ToolStripItemCollection items = newPageMenuItem.DropDownItems; int itemCount = items.Count; for (int i = 0; i < itemCount; i++) { object[] itemInfo = (object[])items[i].Tag; DomainClassInfo diagramInfo = (DomainClassInfo)itemInfo[1]; ReadOnlyCollection <ModelElement> modelElements = partition.ElementDirectory.FindElements(diagramInfo); DiagramMenuDisplayAttribute attribute = (DiagramMenuDisplayAttribute)itemInfo[0]; if (modelElements.Count > 0 & (attribute.DiagramOption & DiagramMenuDisplayOptions.AllowMultiple) == 0) { //DISABLE NEW items[i].Enabled = false; } else { //ENABLE NEW items[i].Enabled = true; } } // Disable page reordering if there is only one page string reorderKey = ResourceStrings.DiagramCommandReorderPages; if (menuItems.ContainsKey(reorderKey)) { menuItems[reorderKey].Enabled = HasMultiplePages; } //If a diagram tab is selected if (diagram != null) { //Retrieve all existing diagrams of the same type as the one selected ReadOnlyCollection <ModelElement> modelElements = partition.ElementDirectory.FindElements(diagram.GetDomainClass(), true); DomainClassInfo diagramInfo = diagram.GetDomainClass(); //Grab the attribute of the diagram type selected object[] attributes = diagramInfo.ImplementationClass.GetCustomAttributes(typeof(DiagramMenuDisplayAttribute), false); if (attributes.Length > 0) { DiagramMenuDisplayAttribute attribute = (DiagramMenuDisplayAttribute)attributes[0]; //If required but you only have 1 then disable delete if ((attribute.DiagramOption & DiagramMenuDisplayOptions.Required) != 0 && modelElements.Count <= 1) { //DISABLE DELETE ToolStripMenuItem deletePageMenuItem = (ToolStripMenuItem)menuItems[ResourceStrings.DiagramCommandDeletePage]; deletePageMenuItem.Enabled = false; } else { //ALLOW DELETE ToolStripMenuItem deletePageMenuItem = (ToolStripMenuItem)menuItems[ResourceStrings.DiagramCommandDeletePage]; deletePageMenuItem.Enabled = true; } if ((attribute.DiagramOption & DiagramMenuDisplayOptions.BlockRename) != 0) { //DISABLE RENAME ToolStripMenuItem renamePageMenuItem = (ToolStripMenuItem)menuItems[ResourceStrings.DiagramCommandRenamePage]; renamePageMenuItem.Enabled = false; } else { //ALLOW RENAME ToolStripMenuItem renamePageMenuItem = (ToolStripMenuItem)menuItems[ResourceStrings.DiagramCommandRenamePage]; renamePageMenuItem.Enabled = true; } } } }