Sdílet prostřednictvím


RemoveAt

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Removes the object at the specified index from a collection.

retval = object.RemoveAt(index)

Arguments

index

object

The zero-based index of the object to remove.

Return Value

Type: object

A reference to the item removed from the collection if successful; otherwise, returns null.

Managed Equivalent

Each collection type potentially implements this differently. The most common equivalent for Silverlight programming is PresentationFrameworkCollection<T>.RemoveAt.

Remarks

The RemoveAt method removes a child object at a specified index value from the parent's collection. This means that the child object does not require an x:Name attribute value. As soon as objects are removed from the Silverlight object hierarchy, they are no longer rendered.

Example

The following JavaScript example shows how to remove the first object from a parent Canvas object by using the RemoveAt method.

// Remove the first child object from the parent collection.
myCanvas.children.removeAt(0);

The Remove method removes a child object from the parent's collection by referencing the object's x:Name / Name attribute value. The following JavaScript example shows how to remove a TextBlock from its parent Canvas object by using the Remove method on the object's collection of children.

function removeCaption(rootCanvas)
{
    // Retrieve the TextBlock object.
    var captionTextBlock = rootCanvas.findName("myCaption");

    if (captionTextBlock != null)
    {
        rootCanvas.children.remove(captionTextBlock);
    }
}

You can remove all objects from a collection by using the Clear method, which is equivalent to using the RemoveAt method for each item in the collection. The following JavaScript example removes all items from a collection by using the Clear method.

// Remove all child objects from the parent collection.
myCanvas.children.clear();

See Also

Reference