ListView.SearchForVirtualItem 事件

定义

ListView 处于虚拟模式下且正进行搜索时发生。

public:
 event System::Windows::Forms::SearchForVirtualItemEventHandler ^ SearchForVirtualItem;
public event System.Windows.Forms.SearchForVirtualItemEventHandler SearchForVirtualItem;
public event System.Windows.Forms.SearchForVirtualItemEventHandler? SearchForVirtualItem;
member this.SearchForVirtualItem : System.Windows.Forms.SearchForVirtualItemEventHandler 
Public Custom Event SearchForVirtualItem As SearchForVirtualItemEventHandler 

事件类型

示例

下面的代码示例演示了此成员的用法。 在示例中,搜索返回与前一万个平方的列表中的指定整数最接近的匹配项。 此代码示例是为 属性提供的更大示例的 VirtualMode 一部分。

//This event handler enables search functionality, and is called
//for every search request when in Virtual mode.
void listView1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
{
    //We've gotten a search request.
    //In this example, finding the item is easy since it's
    //just the square of its index.  We'll take the square root
    //and round.
    double x = 0;
    if (Double.TryParse(e.Text, out x)) //check if this is a valid search
    {
        x = Math.Sqrt(x);
        x = Math.Round(x);
        e.Index = (int)x;
    }
    //If e.Index is not set, the search returns null.
    //Note that this only handles simple searches over the entire
    //list, ignoring any other settings.  Handling Direction, StartIndex,
    //and the other properties of SearchForVirtualItemEventArgs is up
    //to this handler.
}
'This event handler enables search functionality, and is called
'for every search request when in Virtual mode.
Private Sub listView1_SearchForVirtualItem(ByVal sender As Object, ByVal e As SearchForVirtualItemEventArgs) Handles listView1.SearchForVirtualItem
    'We've gotten a search request.
    'In this example, finding the item is easy since it's
    'just the square of its index.  We'll take the square root
    'and round.
    Dim x As Double = 0
    If [Double].TryParse(e.Text, x) Then 'check if this is a valid search
        x = Math.Sqrt(x)
        x = Math.Round(x)
        e.Index = Fix(x)
    End If
    'Note that this only handles simple searches over the entire
    'list, ignoring any other settings.  Handling Direction, StartIndex,
    'and the other properties of SearchForVirtualItemEventArgs is up
    'to this handler.
End Sub

注解

当 处于虚拟模式并且调用 或 FindItemWithText 方法时ListViewFindNearestItem会发生此事件。 处理此事件时,应计算 属性提供的 Items 项列表中的哪个项与搜索条件匹配,并将 属性 SearchForVirtualItemEventArgs.Index 设置为 的 ListViewItem索引。 如果未提供项, FindNearestItem 则 和 FindItemWithText 将返回 null

有关处理事件的详细信息,请参阅 处理和引发事件

适用于