Skip to main content
.NET Framework Class Library
UIElementCollectionIndexOf Method

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

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
Syntax
Public Overridable Function IndexOf ( _
	element As [%$TOPIC/ms590018_en-us_VS_110_2_0_0_0_0%] _
) As [%$TOPIC/ms590018_en-us_VS_110_2_0_0_0_1%]
public virtual [%$TOPIC/ms590018_en-us_VS_110_2_0_1_0_0%] IndexOf(
	[%$TOPIC/ms590018_en-us_VS_110_2_0_1_0_1%] element
)
public:
virtual [%$TOPIC/ms590018_en-us_VS_110_2_0_2_0_0%] IndexOf(
	[%$TOPIC/ms590018_en-us_VS_110_2_0_2_0_1%]^ element
)
abstract IndexOf : 
        element:[%$TOPIC/ms590018_en-us_VS_110_2_0_3_0_0%] -> [%$TOPIC/ms590018_en-us_VS_110_2_0_3_0_1%]  
override IndexOf : 
        element:[%$TOPIC/ms590018_en-us_VS_110_2_0_3_0_2%] -> [%$TOPIC/ms590018_en-us_VS_110_2_0_3_0_3%]
public function IndexOf(
	element : [%$TOPIC/ms590018_en-us_VS_110_2_0_4_0_0%]
) : [%$TOPIC/ms590018_en-us_VS_110_2_0_4_0_1%]

Parameters

element
Type: System.WindowsUIElement

The element whose index position is required.

Return Value

Type: SystemInt32
The index position of the element. -1 if the element is not in the collection.
Examples

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>
    </DockPanel>
</DockPanel>

...

Dim c_counter as Integer = 0
    Private Sub FindIndex(ByVal sender As Object, ByVal args As RoutedEventArgs)
        Try 
            Dim newText As TextBlock = New TextBlock()
            c_counter = c_counter + 1
            ' Add this element to the UIElementCollection of the DockPanel element.
            MainDisplayPanel.Children.Add(newText)
            ' 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
            System.Windows.MessageBox.Show(ex.Message)
        End Try 
    End Sub
private int c_counter = 0;
		void FindIndex(object sender, RoutedEventArgs e)
		{
			c_counter+=1;
			// Create a new Text element.
			TextBlock newText = new TextBlock();
			// Add this element to the UIElementCollection of the DockPanel element.
			MainDisplayPanel.Children.Add(newText);
			// Add a text node under the Text element. This text is displayed. 
			newText.Text = "New element #" + 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);
		}
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1