コード例 #1
0
        public void Insert()
        {
            ControlCollection controlCollection = new ControlCollection(new Control());
            Control           newControl        = new Control();
            Control           controlWithName   = new Control
            {
                Name = "Special Control"
            };

            // Test insert while out of bounds
            try
            {
                controlCollection.Insert(5, newControl);
                Assert.Fail("Insert failed to throw an IndexOutOfRangeException");
            }
            catch (IndexOutOfRangeException) { }

            // Test insert with no other controls currently in collection
            controlCollection.Insert(0, newControl);
            Assert.AreEqual(1, controlCollection.Count, DebugUtilities.FailedAssertMessage("Insert()", 2, 1, controlCollection.Count));

            for (int i = 0; i < 10; ++i)
            {
                controlCollection.Add(new Control());
            }

            // Test Basic Insert
            controlCollection.Insert(4, controlWithName);
            Assert.AreEqual(controlWithName.Name, controlCollection[4], DebugUtilities.FailedAssertMessage("Insert()", 2, controlWithName.Name, controlCollection[4]));
            controlCollection.Remove(controlWithName);

            // Test Smart Insert
            controlCollection.Insert(8, controlWithName);
            Assert.AreEqual(controlWithName.Name, controlCollection[8], DebugUtilities.FailedAssertMessage("Insert()", 3, controlWithName.Name, controlCollection[8]));
            controlCollection.Remove(controlWithName);
        }
コード例 #2
0
 /// <summary>
 /// Replaces a specific index in a control collection
 /// </summary>
 /// <param name="originalCollection">The collection that is being modified</param>
 /// <param name="indexToReplace">The index to be replaced</param>
 /// <param name="newControl">The new control to replace the old control</param>
 public static void Replace(this ControlCollection originalCollection, int indexToReplace, Control newControl)
 {
     originalCollection.Insert(indexToReplace, newControl);
     originalCollection.RemoveAt(indexToReplace + 1);
 }