/// <summary> /// Initializes a new instance of the /// <see cref="T:DesktopSprites.SpriteManagement.WinFormSpriteInterface.WinFormContextMenuItem"/> class for the given /// <see cref="T:System.Windows.Forms.ToolStripMenuItem"/>, which is assumed to produce a sub-menu of the given sub-items. /// </summary> /// <param name="parent">The <see cref="T:DesktopSprites.SpriteManagement.WinFormSpriteInterface"/> that will own this /// <see cref="T:DesktopSprites.SpriteManagement.WinFormSpriteInterface.WinFormContextMenuItem"/>.</param> /// <param name="menuItem">The underlying <see cref="T:System.Windows.Forms.ToolStripMenuItem"/> that this class wraps.</param> /// <param name="subItems">The items to appear in the sub-menu.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="parent"/> is null.-or-<paramref name="menuItem"/> is null. /// -or-<paramref name="subItems"/> is null.</exception> /// <exception cref="T:System.ArgumentException"><paramref name="menuItem"/> does not have drop down items.-or-The number of /// drop down items in <paramref name="menuItem"/> does not match the number of <paramref name="subItems"/>.</exception> public WinFormContextMenuItem(WinFormSpriteInterface parent, ToolStripMenuItem menuItem, IEnumerable<ISimpleContextMenuItem> subItems) { Argument.EnsureNotNull(parent, "parent"); Argument.EnsureNotNull(menuItem, "menuItem"); Argument.EnsureNotNull(subItems, "subItems"); if (!menuItem.HasDropDownItems) throw new ArgumentException("menuItem must have drop down items.", "menuItem"); var subItemsArray = subItems.ToArray(); if (menuItem.DropDownItems.Count != subItemsArray.Length) throw new ArgumentException( "The number of sub-items in menuItem is not the same as the number in the subItems collection."); var winFormSubItemsList = new ISimpleContextMenuItem[subItemsArray.Length]; int index = 0; foreach (ToolStripItem toolStripItem in menuItem.DropDownItems) { if (subItemsArray[index].IsSeparator) winFormSubItemsList[index] = new WinFormContextMenuItem(parent, (ToolStripSeparator)toolStripItem); else if (subItemsArray[index].SubItems == null) winFormSubItemsList[index] = new WinFormContextMenuItem(parent, (ToolStripMenuItem)toolStripItem, subItemsArray[index].Activated); else winFormSubItemsList[index] = new WinFormContextMenuItem(parent, (ToolStripMenuItem)toolStripItem, subItemsArray[index].SubItems); index++; } SubItems = winFormSubItemsList; owner = parent; item = menuItem; }
/// <summary> /// Creates a <see cref="T:System.Windows.Forms.ToolStripMenuItem"/> from a specified /// <see cref="T:DesktopSprites.SpriteManagement.ISimpleContextMenuItem"/> that has sub items. If necessary, recursively calls /// itself to create another sub-menu for a sub-item that itself has sub-items. /// </summary> /// <param name="menuItem">The <see cref="T:DesktopSprites.SpriteManagement.ISimpleContextMenu"/> for which to create a /// <see cref="T:System.Windows.Forms.ToolStripMenuItem"/>.</param> /// <returns>A <see cref="T:System.Windows.Forms.ToolStripMenuItem"/> whose tree of sub-menus is fully initialized.</returns> /// <exception cref="T:System.ArgumentNullException"><paramref name="menuItem"/> is null.</exception> /// <exception cref="T:System.ArgumentException">The sub-items collection in <paramref name="menuItem"/> is null or empty. /// </exception> private ToolStripMenuItem CreateItemWithSubitems(ISimpleContextMenuItem menuItem) { Argument.EnsureNotNull(menuItem, "menuItems"); if (menuItem.SubItems == null || menuItem.SubItems.Count == 0) throw new ArgumentException("menuItem.SubItems must not be null or empty."); ToolStripItem[] subitems = new ToolStripItem[menuItem.SubItems.Count]; for (int i = 0; i < subitems.Length; i++) { ISimpleContextMenuItem subitem = menuItem.SubItems[i]; ToolStripItem winFormMenuItem; if (subitem.IsSeparator) winFormMenuItem = new ToolStripSeparator(); else if (subitem.SubItems == null) winFormMenuItem = new ToolStripMenuItem(subitem.Text); else winFormMenuItem = CreateItemWithSubitems(subitem); subitems[i] = winFormMenuItem; } return new ToolStripMenuItem(menuItem.Text, null, subitems); }