# Searching Items

Better ListView: Ultimate .NET ListView replacement control for WinForms (C#, VB.NET)
Download More Info

Better ListView offers many options for searching items by typing on keyboard and programmaticaly (FindItemWithText, FindItemsWithText methods). Search can be extended to sub-items and event multiple items can be contained in a search result.

Search can be customized with SearchSettings property. This structure contains three other properties:

- Mode

  - Specifies how the searching is done on each item/sub-item string.

    - Disabled

      - Searching is disabled.

    - Prefix

      - Search is restricted to text prefix.

    - PrefixOrSubstring

      - Search query can match any substring, if nothing found by prefix.

    - Substring

      - Search query can match any substring.

- Options

  - Additional options for searching.

    - CaseSensitive

      - The search is case-sensitive.

    - FirstWordOnly

      - Search is restricted to the first word of searched text.

    - None

      - No options active.

    - PlaySound

      - Sound is played, when nothing is found.

    - PrefixPreference

      - Results matched by prefix are prefered among other (e.g. when searching for pla, then the text player is prefered among the word applause).

    - SelectableItemsOnly

      - Restrict the search to selectable items only.

    - WordSearch

      - Searched text si first split into words and searching is done on each word separately.

- SubItemIndices

  - Specifies sub-items on which the search is done; if the collection is empty, than all sub-items are searched.

There is a default one-second delay to register when user stopped typing and the search is discarded. A new search is initiated when user starts typing after this interval has passed. This interval can be set via SearchTimeoutDelay property.

The delay is not relevant when user types the same letter several times and there are other items beginning with that letter. If there are items named ab, ac, ad, then the selection cycles through these items as long as the user keeps pressing A key. This works essentialy the same way as in the Windows Explorer.

The keyboard search works, of course, only when the control has focus. You can ensure this (e.g. when showing the form) by calling Focus method on Better ListView.

## Sample Source Code

C#

    this.listView.BeginUpdate();

    // fill the ListView with items in two columns
    this.listView.Columns.AddRange(
        new[]
        {
            new BetterListViewColumnHeader("Word", 128),
            new BetterListViewColumnHeader("Synonym List", 160)
        });

    this.listView.Items.AddRange(
        new[]
        {
            new BetterListViewItem(new[] { "apparently", "evidently, presumably, seemingly" }),
            new BetterListViewItem(new[] { "blunt", "brusque, curt, snippy" }),
            new BetterListViewItem(new[] { "class", "caste, estate, folk" }),
            new BetterListViewItem(new[] { "detailed", "elaborate, full, thorough" }),
        });

    // search in substrings
    BetterListViewSearchMode searchMode = BetterListViewSearchMode.Substring;

    // use case-sensitive searching and play sounds
    BetterListViewSearchOptions searchOptions = (BetterListViewSearchOptions.CaseSensitive | BetterListViewSearchOptions.PlaySound);

    // search in the first and second column
    //NOTE: empty array also means searching in all columns
    int[] subItemIndices = new[] { 0, 1 };

    // set-up the search
    this.listView.SearchSettings = new BetterListViewSearchSettings(searchMode, searchOptions, subItemIndices);

    this.listView.EndUpdate();

Visual Basic

    ListView.BeginUpdate()

    ' fill the ListView with items in two columns
    ListView.Columns.AddRange(
        New BetterListViewColumnHeader() { _
                                             New BetterListViewColumnHeader("Word", 128),
                                             New BetterListViewColumnHeader("Synonym List", 160)
                                         })

    ListView.Items.AddRange(
        New BetterListViewItem() { _
                                     New BetterListViewItem(New String() _
                                                                {"apparently", "evidently, presumably, seemingly"}),
                                     New BetterListViewItem(New String() {"blunt", "brusque, curt, snippy"}),
                                     New BetterListViewItem(New String() {"class", "caste, estate, folk"}),
                                     New BetterListViewItem(New String() {"detailed", "elaborate, full, thorough"})
                                })

    ' search in substrings
    Dim searchMode As BetterListViewSearchMode = BetterListViewSearchMode.Substring

    ' use case-sensitive searching and play sounds
    Dim searchOptions As BetterListViewSearchOptions =
            (BetterListViewSearchOptions.CaseSensitive Or BetterListViewSearchOptions.PlaySound)

    ' search in the first and second column
    'NOTE: empty array also means searching in all columns
    Dim subItemIndices As Integer() = New Integer() {0, 1}

    ' set-up the search
    ListView.SearchSettings = New BetterListViewSearchSettings (searchMode, searchOptions, subItemIndices)

    ListView.EndUpdate()

Better ListView: Ultimate .NET ListView replacement control for WinForms (C#, VB.NET)
Download More Info