ListViewItem.FindNearestItem(SearchDirectionHint) Method


Finds the next item from the ListViewItem, searching in the specified direction.

 System::Windows::Forms::ListViewItem ^ FindNearestItem(System::Windows::Forms::SearchDirectionHint searchDirection);
public System.Windows.Forms.ListViewItem FindNearestItem (System.Windows.Forms.SearchDirectionHint searchDirection);
member this.FindNearestItem : System.Windows.Forms.SearchDirectionHint -> System.Windows.Forms.ListViewItem
Public Function FindNearestItem (searchDirection As SearchDirectionHint) As ListViewItem



One of the SearchDirectionHint values.



The ListViewItem that is closest to the given coordinates, searching in the specified direction.


The View property of the containing ListView is set to a value other than SmallIcon or LargeIcon.


The following code example demonstrates how to use the FindNearestItem method. To run this example, paste the following code into a Windows Form that contains a ListView named findListView. Ensure that the View property is set to an icon view and that the ListView is populated with items. Associate the MouseDown event of findListView with the findListView_MouseDown method in this example.

void findListView_MouseDown(object sender, MouseEventArgs e)
    ListViewHitTestInfo info = findListView.HitTest(e.X, e.Y);
    ListViewItem foundItem = null;
    if (info.Item != null)
        foundItem = info.Item.FindNearestItem(SearchDirectionHint.Up);
    if (foundItem != null)
        label1.Text = "Previous Item: " + foundItem.Text;

        label1.Text = "No item found";
Private Sub findListView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

    Dim info As ListViewHitTestInfo = findListView.HitTest(e.X, e.Y)
    Dim foundItem As ListViewItem = Nothing
    If (info.Item IsNot Nothing) Then
        foundItem = info.Item.FindNearestItem(SearchDirectionHint.Up)
    End If
    If (foundItem IsNot Nothing) Then
        label1.Text = "Previous Item: " + foundItem.Text

        label1.Text = "No item found"
    End If

End Sub


The FindNearestItem method returns null if no item is found in the given direction.

Identifying the nearest item can vary depending on the operating system the application is running on and will affect the results of FindNearestItem.

Applies to