/// <include file='doc\Menu.uex' path='docs/doc[@for="Menu.MenuItemCollection.Add4"]/*' /> /// <devdoc> /// Adds a MenuItem to this menu at the specified index. The item currently at /// that index, and all items after it, will be moved up one slot. /// MenuItems can only be contained in one menu at a time, and may not be added /// more than once to the same menu. /// </devdoc> public virtual int Add(int index, MenuItem item) { // MenuItems can only belong to one menu at a time if (item.menu != null) { // First check that we're not adding ourself, i.e. walk // the parent chain for equality if (owner is MenuItem) { MenuItem parent = (MenuItem)owner; while (parent != null) { if (parent.Equals(item)) { throw new ArgumentException(SR.GetString(SR.MenuItemAlreadyExists, item.Text), "item"); } if (parent.Parent is MenuItem) { parent = (MenuItem)parent.Parent; } else { break; } } } item.menu.MenuItems.Remove(item); } if (index < 0 || index > owner.itemCount) { throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidArgument, "index", (index).ToString())); } if (owner.items == null || owner.items.Length == owner.itemCount) { MenuItem[] newItems = new MenuItem[owner.itemCount < 2? 4: owner.itemCount * 2]; if (owner.itemCount > 0) { System.Array.Copy(owner.items, 0, newItems, 0, owner.itemCount); } owner.items = newItems; } System.Array.Copy(owner.items, index, owner.items, index + 1, owner.itemCount - index); owner.items[index] = item; owner.itemCount++; item.menu = owner; owner.ItemsChanged(CHANGE_ITEMS); return(index); }
/// <include file='doc\Menu.uex' path='docs/doc[@for="Menu.MenuItemCollection.Add4"]/*' /> /// <devdoc> /// Adds a MenuItem to this menu at the specified index. The item currently at /// that index, and all items after it, will be moved up one slot. /// MenuItems can only be contained in one menu at a time, and may not be added /// more than once to the same menu. /// </devdoc> public virtual int Add(int index, MenuItem item) { // MenuItems can only belong to one menu at a time if (item.Menu != null) { // First check that we're not adding ourself, i.e. walk // the parent chain for equality if (owner is MenuItem) { MenuItem parent = (MenuItem)owner; while (parent != null) { if (parent.Equals(item)) { throw new ArgumentException(string.Format(SR.MenuItemAlreadyExists, item.Text), "item"); } if (parent.Parent is MenuItem) { parent = (MenuItem)parent.Parent; } else { break; } } } //if we're re-adding an item back to the same collection //the target index needs to be decremented since we're //removing an item from the collection if (item.Menu.Equals(owner) && index > 0) { index--; } item.Menu.MenuItems.Remove(item); } // Validate our index if (index < 0 || index > owner.ItemCount) { throw new ArgumentOutOfRangeException("index", string.Format(SR.InvalidArgument, "index", (index).ToString(CultureInfo.CurrentCulture))); } if (owner.items == null || owner.items.Length == owner.ItemCount) { MenuItem[] newItems = new MenuItem[owner.ItemCount < 2? 4: owner.ItemCount * 2]; if (owner.ItemCount > 0) { System.Array.Copy(owner.items, 0, newItems, 0, owner.ItemCount); } owner.items = newItems; } System.Array.Copy(owner.items, index, owner.items, index + 1, owner.ItemCount - index); owner.items[index] = item; owner._itemCount++; item.Menu = owner; owner.ItemsChanged(CHANGE_ITEMS); if (owner is MenuItem) { ((MenuItem)owner).ItemsChanged(CHANGE_ITEMADDED, item); } return(index); }