Edit

Share via


ListViewItem.FindNearestItem(SearchDirectionHint) Method

Definition

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

public:
 System::Windows::Forms::ListViewItem ^ FindNearestItem(System::Windows::Forms::SearchDirectionHint searchDirection);
public 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

Parameters

searchDirection
SearchDirectionHint

One of the SearchDirectionHint values.

Returns

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

Exceptions

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

Examples

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;

    else
        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

    Else
        label1.Text = "No item found"
    End If

End Sub

Remarks

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