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);

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";
}

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

Toode Versioonid
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9