高级搜索列表视图

时间:2019-07-10 14:46:44

标签: c# winforms

我的搜索按钮现在允许在listView中进行常规搜索。它不允许我通过显示在文本框中键入的“完全匹配”进行搜索,而不包括其余内容。

private void SearchBtn_Click(object sender, EventArgs e)
{
    int count = 0, searchStartIndex = 0;
    // Clear previously selected indices
    listView.SelectedIndices.Clear();
    string target = searchTextBox.Text;
    // Search for item with text from the search text box, including subItems, from searchStartIndex, not a prefixSearch
    ListViewItem item = listView.FindItemWithText(target, true, searchStartIndex, false);
    while (item != null)
    {
        count++;
        // Update progressBar
        progressBar.Value = (int)((float)searchStartIndex / listView.VirtualListSize * 100);
        ListView.SelectedIndexCollection indexes = listView.SelectedIndices;
        if (!indexes.Contains(item.Index))
        {
            listView.SelectedIndices.Add(item.Index);
        }
        if ((searchStartIndex = item.Index + 1) < listView.VirtualListSize)
        {
            item = listView.FindItemWithText(searchTextBox.Text, true, searchStartIndex, false);
            //  count++;
        }
        else
        {
            item = null;
        }
    }
    if (listView.SelectedIndices.Count == 0)
    {
        MessageBox.Show("Find item with text \"" + searchTextBox.Text + "\" has no result.");
    }
    else
    {
        RefilterListView();
        listView.EnsureVisible(listView.SelectedIndices[0]);
    }
    // Reset focus on the listView so the user can see the selected indices
    listView.Focus();
    // Update searchLabel with count of number found
    searchLabel.Text = "" + count + " results found";
}

我希望能够使用两个单选按钮进行搜索:“显示匹配”将仅显示搜索到的项目,“显示不匹配”将仅显示与搜索项目无关的项目。

0 个答案:

没有答案