/// <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> /// Initializes a new instance of the /// <see cref="T:DesktopSprites.SpriteManagement.WinFormSpriteInterface.WinFormContextMenuItem"/> class for the given /// <see cref="T:System.Windows.Forms.ToolStripSeparator"/>. /// </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="separatorItem">The underlying <see cref="T:System.Windows.Forms.ToolStripSeparator"/> that this class wraps. /// </param> /// <exception cref="T:System.ArgumentNullException"><paramref name="parent"/> is null.-or-<paramref name="separatorItem"/> is /// null.</exception> public WinFormContextMenuItem(WinFormSpriteInterface parent, ToolStripSeparator separatorItem) { Argument.EnsureNotNull(parent, "parent"); Argument.EnsureNotNull(separatorItem, "separatorItem"); owner = parent; separator = separatorItem; }
/// <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"/>, and links up the given activation method. /// </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="activated">The method to be run when the item is activated.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="parent"/> is null.-or-<paramref name="menuItem"/> is null. /// </exception> public WinFormContextMenuItem(WinFormSpriteInterface parent, ToolStripMenuItem menuItem, EventHandler activated) { Argument.EnsureNotNull(parent, "parent"); Argument.EnsureNotNull(menuItem, "menuItem"); owner = parent; item = menuItem; Activated = activated; }
/// <summary> /// Initializes a new instance of the /// <see cref="T:DesktopSprites.SpriteManagement.WinFormSpriteInterface.WinFormContextMenu"/> class to display the given menu /// items. /// </summary> /// <param name="parent">The <see cref="T:DesktopSprites.SpriteManagement.WinFormSpriteInterface"/> that will own this /// <see cref="T:DesktopSprites.SpriteManagement.WinFormSpriteInterface.WinFormContextMenu"/>.</param> /// <param name="menuItems">The items which should be displayed in this menu.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="parent"/> is null.-or-<paramref name="menuItems"/> is /// null.</exception> public WinFormContextMenu(WinFormSpriteInterface parent, IEnumerable<ISimpleContextMenuItem> menuItems) { Argument.EnsureNotNull(parent, "parent"); Argument.EnsureNotNull(menuItems, "menuItems"); owner = parent; var items = new List<ISimpleContextMenuItem>(); Items = items; foreach (ISimpleContextMenuItem menuItem in menuItems) { ToolStripItem winFormMenuItem; WinFormContextMenuItem winFormContextMenuItem; if (menuItem.IsSeparator) { winFormMenuItem = new ToolStripSeparator(); winFormContextMenuItem = new WinFormContextMenuItem(parent, (ToolStripSeparator)winFormMenuItem); } else if (menuItem.SubItems == null) { winFormMenuItem = new ToolStripMenuItem(menuItem.Text); winFormContextMenuItem = new WinFormContextMenuItem(parent, (ToolStripMenuItem)winFormMenuItem, menuItem.Activated); } else { winFormMenuItem = CreateItemWithSubitems(menuItem); winFormContextMenuItem = new WinFormContextMenuItem(parent, (ToolStripMenuItem)winFormMenuItem, menuItem.SubItems); } base.Items.Add(winFormMenuItem); items.Add(winFormContextMenuItem); } Disposed += (sender, e) => { foreach (WinFormContextMenuItem item in Items) item.Dispose(); }; }