ListViewItem.FindNearestItem Method
Finds the next item from the ListViewItem, searching in the specified direction.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Parameters
- searchDirection
- Type: System.Windows.Forms.SearchDirectionHint
One of the SearchDirectionHint values.
Return Value
Type: System.Windows.Forms.ListViewItemThe ListViewItem that is closest to the given coordinates, searching in the specified direction.
| Exception | Condition |
|---|---|
| InvalidOperationException |
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; else label1.Text = "No item found"; }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.