HypernovaDesktop Components · docs
Binarymission
WinForms › Search Text Box

Search Text Box

.NET Framework 4.8.NET 6 / 8 / 10v3.0.0WinForms

A one-line search box with a grey prompt and three buttons drawn inside its right-hand end – a magnifier to run the search, a cross to clear it, and a chevron to open a list such as recent searches.

3public types
31public members
1namespaces

Assembly

AssemblyBinarymission.WinForms.Controls.SearchTextBox.dll
Root namespaceBinarymission.WinForms.Controls.InputControls
Target frameworks.NET Framework 4.8, .NET 6 / 8 / 10
Depends onBinaryExtendedText

Overview

What it is for

Use it at the top of a list, a grid or a document view, wherever the user filters or finds something: "Search customers", "Find in orders". It is a BinaryExtendedText, so it has the flat coloured border, hover effect and blinking of that control, plus the search furniture.

How it works

The control does no searching itself. Each button raises an event and leaves the work to the application:

ButtonEvent

Magnifier

TextSearchRequested, carrying the text to search for.

Cross

ClearSearchTextRequested – the handler clears the box and the results.

Chevron

SearchDropListOpenRequested, carrying where to show a list; set IsSearchDropListOpenRequestCompleted back to true when the list closes.

The prompt (CueText) is the Windows cue banner, shown in grey while the box is empty. Set IsSearchCommandSetEnabled to false to keep only the chevron, or IsOnlyWatermarkProvider to true for a plain box with a prompt.

Appearance

The buttons are drawn as vector glyphs, sharp at every display scale, in NormalColorForSearchDropListChevron; the chevron turns HoverColorForSearchDropListChevron under the pointer. Each can be replaced with a picture of your own (SearchCommandInvokerImage, ClearSearchTextCommandInvokerImage, SearchDropListCommandInvokerImage). Each button is as wide as 1.2 times the font height, and at least 24 logical pixels; the text never runs under them.

Tip

Pressing Enter does not raise TextSearchRequested. To search on Enter as well, handle Control.KeyDown, as the example does.

Getting started

using System;
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.InputControls;

public class CustomerListForm : Form
{
    private readonly SearchTextBox searchBox;
    private readonly ListBox results;
    private readonly ContextMenuStrip recentSearches = new ContextMenuStrip();
    private readonly string[] customers = { "Acme Trading", "Brightwell Ltd", "Carter & Sons", "Northwind Foods" };

    public CustomerListForm()
    {
        Text = "Customers";
        ClientSize = new Size(360, 300);

        searchBox = new SearchTextBox
        {
            Location = new Point(12, 12),
            Width = 336,
            CueText = "Search customers by name",
            NormalColorForSearchDropListChevron = Color.DimGray,
            HoverColorForSearchDropListChevron = Color.SteelBlue
        };
        searchBox.TextSearchRequested += (sender, e) => RunSearch(e.SearchText);
        searchBox.ClearSearchTextRequested += (sender, e) => { searchBox.Clear(); RunSearch(""); };
        searchBox.SearchDropListOpenRequested += SearchBox_SearchDropListOpenRequested;
        searchBox.KeyDown += (sender, e) =>
        {
            if (e.KeyCode == Keys.Enter) { RunSearch(searchBox.Text); e.SuppressKeyPress = true; }
        };

        recentSearches.Items.Add("Northwind");
        recentSearches.Items.Add("Ltd");
        recentSearches.ItemClicked += (sender, e) => { searchBox.Text = e.ClickedItem.Text; RunSearch(e.ClickedItem.Text); };
        recentSearches.Closed += (sender, e) => searchBox.IsSearchDropListOpenRequestCompleted = true;

        results = new ListBox { Location = new Point(12, 44), Size = new Size(336, 244) };

        Controls.Add(searchBox);
        Controls.Add(results);
        RunSearch("");
    }

    private void SearchBox_SearchDropListOpenRequested(object sender, SearchTextBoxDropListInvokedEventArgs e)
    {
        // Show the list under the chevron.
        recentSearches.Show(searchBox, new Point(e.DropListInvokerTopLeftPoint.X, searchBox.Height));
    }

    private void RunSearch(string text)
    {
        results.Items.Clear();
        foreach (string name in customers)
        {
            if (name.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
            {
                results.Items.Add(name);
            }
        }
    }
}

API reference

Binarymission.WinForms.Controls.InputControls

class SearchTextBoxA one-line search box with a grey prompt and three buttons drawn inside its right-hand end - a magnifier to run the search, a cross to clear it, and a chevron to open a list such as recent searches.
class SearchTextBoxDropListInvokedEventArgsProvides data for the SearchDropListOpenRequested event: where the chevron is, so the list can be shown beside it.
class SearchTextBoxRequestEventArgsProvides data for the TextSearchRequested event: the text to search for.