Расширенный поиск просмотра списка - PullRequest
0 голосов
/ 10 июля 2019

Моя кнопка поиска теперь позволяет выполнять обычный поиск в 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";
}

Я ожидаю, что смогу выполнять поиск с помощью двух переключателей: «показать совпадение», которые будут отображать только найденные элементы, и «показывать отсутствие совпадения», которые будут отображать только элементы, не связанные с поисковыми элементами.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...