Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

UIElementCollection.IndexOf Method (UIElement)


Returns the index position of a specified element in a UIElementCollection.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

Public Overridable Function IndexOf (
	element As UIElement
) As Integer


Type: System.Windows.UIElement

The element whose index position is required.

Return Value

Type: System.Int32

The index position of the element. -1 if the element is not in the collection.

The following example finds the index position of a UIElement within a UIElementCollection.

<DockPanel Name="ParentElement">

    <TextBlock DockPanel.Dock="Top" Name="TxtDisplay"></TextBlock>

    <Button DockPanel.Dock="Top" Click="FindIndex">What is the Index Number
        of the Element Just Added?</Button>
    <DockPanel Name="MainDisplayPanel">
        <TextBlock DockPanel.Dock="Top">Text 1</TextBlock>
        <TextBlock DockPanel.Dock="Top">Text 2</TextBlock>


Dim c_counter as Integer = 0
   Private Sub FindIndex(ByVal sender As Object, ByVal args As RoutedEventArgs)
           Dim newText As TextBlock = New TextBlock()
           c_counter = c_counter + 1
           ' Add this element to the UIElementCollection of the DockPanel element.
           ' Add a text node under the Text element. This text is displayed. 
           newText.Text = "New element #" & CStr(c_counter)
           DockPanel.SetDock(newText, Dock.Top)
           ' Display the Index number of the new element.    
           TxtDisplay.Text = "The Index of the new element is " & MainDisplayPanel.Children.IndexOf(newText)
       Catch ex As System.Exception
       End Try
   End Sub

.NET Framework
Available since 3.0
Return to top
© 2017 Microsoft