How to: Remove an Element by Using an Index

This example shows how to remove a Button element by using its index number and the MouseLeftButtonUp event handler.

Example

<TabItem MouseLeftButtonUp="RemoveButton">
<TabItem.Header>Remove Control</TabItem.Header>
</TabItem>

...

        void RemoveButton(object sender, MouseButtonEventArgs e)
        {
            if ((sp1.Children.IndexOf(btn) >= 0) || (sp1.Children.IndexOf(btn1) >= 0) || (sp1.Children.IndexOf(btn2) >= 0) || (sp1.Children.IndexOf(btn3) >= 0))
            {
                sp1.Children.RemoveAt(0);
            }
        }
Private Sub RemoveButton(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
    If (sp1.Children.IndexOf(btn) >= 0) Or (sp1.Children.IndexOf(btn1) >= 0) Or (sp1.Children.IndexOf(btn2) >= 0) Or (sp1.Children.IndexOf(btn3) >= 0) Then
        sp1.Children.RemoveAt(0)
    End If 
End Sub
<TabItem MouseLeftButtonUp="ClearButtons">
<TabItem.Header>Clear Controls</TabItem.Header>
</TabItem>

For the complete sample, see Using Elements Sample.

See Also

Tasks

Using Elements Sample

Reference

UIElementCollection