/// <summary>Inserts MenuItem at given position in given menu item collection.</summary> /// <param name="MenuItems"></param> /// <param name="index"></param> /// <param name="menuItem"></param> public static void Insert(Menu.MenuItemCollection MenuItems, int index, MenuItem menuItem) { // If index is > count add the menuItem if (MenuItems.Count <= index + 1) { MenuItems.Add(menuItem); } else { //Otherwise copy all the menu items to an array MenuItem[] NewMenuItems = new MenuItem[MenuItems.Count]; MenuItems.CopyTo(NewMenuItems, 0); //Remove all the menuitems where we want to insert the element. for (int i = index; i < NewMenuItems.Length; i++) { MenuItems.RemoveAt(i); } //Add the menu item MenuItems.Add(menuItem); // Add remianing menu Items. for (int i = index; index < NewMenuItems.Length; index++) { MenuItems.Add(NewMenuItems[i]); } } }
public void MenuItemCollection_CopyTo_Empty_Nop() { var menu = new SubMenu(new MenuItem[0]); var collection = new Menu.MenuItemCollection(menu); var array = new object[] { 1, 2, 3 }; collection.CopyTo(array, 1); Assert.Equal(new object[] { 1, 2, 3 }, array); }
public void MenuItemCollection_CopyTo_NotEmpty_Success() { var menuItem = new MenuItem(); var menu = new SubMenu(new MenuItem[0]); var collection = new Menu.MenuItemCollection(menu); collection.Add(menuItem); var array = new object[] { 1, 2, 3 }; collection.CopyTo(array, 1); Assert.Equal(new object[] { 1, menuItem, 3 }, array); }