Share via


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 MouseLeftButtonDown event handler.

The following Extensible Application Markup Language (XAML) example creates a TabControl. When the user clicks the Remove Control tab, a MouseLeftButtonDown event handler, RemoveButton, which is in thecode-behind file, removes the control at an index of zero (0).

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);
    }
}
<TabItem MouseLeftButtonUp="ClearButtons">
<TabItem.Header>Clear Controls</TabItem.Header>
</TabItem>
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

For the complete sample, see Using Elements Sample.

See Also

Reference

UIElementCollection

Other Resources

Using Elements Sample