Skip to main content

FuzzySort

FuzzySort improves the type-ahead search of a Wisej.NET ComboBox. As the user types, the drop-down list keeps only the items that contain the typed text, and the matching characters of each item are highlighted. The matching and highlighting run entirely in the browser, with the fuzzysort library (version 3.1.0), so the list responds instantly with no server round trip.

A ComboBox with "ap" typed in it and its list reduced to Apple, Grape, Pineapple and Apricot, with the letters "ap" in each item highlighted in red

Typing "ap" filters the list and highlights the matching characters.

Installation​

Add the Wisej-4-FuzzySort package to your Wisej.NET project:

dotnet add package Wisej-4-FuzzySort

The package targets .NET Framework 4.8 and .NET 8 or later. It adds no control to the toolbox. Instead, it adds an extension method to the ComboBox you already use.

Getting started​

Fill a ComboBox and call ApplyFuzzySort() on it:

using Wisej.Ext.FuzzySort;

var comboBox = new ComboBox
{
Width = 250,
Location = new Point(20, 20)
};
comboBox.Items.AddRange(new object[]
{
"Apple", "Banana", "Grape", "Pineapple",
"Orange", "Apricot", "Blueberry", "Blackberry"
});

this.Controls.Add(comboBox);

// Enable the fuzzy search. The method returns the same ComboBox, so calls can be chained.
comboBox.ApplyFuzzySort();

For a ComboBox placed in the designer, call ApplyFuzzySort() once in the page's or form's constructor, after InitializeComponent().

How it works​

ApplyFuzzySort() does three things:

  1. It sets the combo box's AutoCompleteMode to Filter, so the drop-down list shows only the items that match what the user types.
  2. It installs a filter that keeps an item when its text contains the typed text anywhere, ignoring case. That's why Grape and Pineapple stay in the list for "ap" in the screenshot.
  3. It runs fuzzysort over the visible items and highlights the matched characters in red.

The items keep their original order in the list. Highlighting marks each match but doesn't sort the list by score.

Keep AutoCompleteMode on Filter

The extension works only in the Filter auto-complete mode. If you set AutoCompleteMode to any other value after calling ApplyFuzzySort(), the fuzzy filtering stops.

note

Highlighting turns the drop-down items into rich (HTML) labels, so any markup in an item's text is rendered rather than shown literally. Don't use this on a ComboBox whose items can contain < or & characters from user input.

API reference​