SearchTextBox
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.
public class SearchTextBox : BinaryExtendedText
Inherits from / implements: BinaryExtendedText
Remarks
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:
| Button | Event |
|---|---|
Magnifier |
|
Cross |
|
Chevron |
|
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.
Pressing Enter does not raise TextSearchRequested. To search on Enter
as well, handle Control.KeyDown, as the example does.
Examples
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);
}
}
}
}See also
Members at a glance
| Constructors | |
|---|---|
SearchTextBox | Initialises a search box showing the prompt "Search..." and all three buttons. |
| Properties | |
ClearSearchTextCommandInvokerImage | Gets or sets a picture to show in place of the drawn cross. |
CueText | Gets or sets the grey prompt shown while the box is empty – "Search orders", "Find a customer". |
DefaultDropListChevronBorderColor | Gets or sets the colour of the outline round the drawn chevron. |
DefaultDropListChevronBorderOnHoverColor | Gets or sets the colour of the chevron's outline while the pointer is over it or its list is open. |
DefaultDropListChevronBorderWidth | Gets or sets the width of the outline round the drawn chevron. |
DefaultDropListChevronRotateFlipType | Gets or sets which way the drawn chevron points. |
HoverColorForSearchDropListChevron | Gets or sets the colour of the drawn chevron while the pointer is over it or its list is open. |
IsCueTextRenderedWhenInFocus | Gets or sets whether the prompt stays visible in the empty box while it has the focus. |
IsOnlyWatermarkProvider | Gets or sets whether the box is a plain text box with a prompt, without its three buttons. |
IsSearchCommandSetEnabled | Gets or sets whether the magnifier and the cross are shown; the chevron is shown either way. |
IsSearchDropListOpenRequestCompleted | Gets or sets whether the list opened from the chevron has been dealt with – closed or dismissed. |
MarginBetweenTextAndCommandInvokers | Gets or sets the space left between the end of the text and the magnifier. |
Multiline | Gets whether the box takes more than one line of text – which a search box never does. |
NormalColorForSearchDropListChevron | Gets or sets the colour the drawn magnifier, cross and chevron are drawn in. |
ScrollBars | Gets which scroll bars the box shows – none, as it holds a single line. |
SearchCommandInvokerImage | Gets or sets a picture to show in place of the drawn magnifier. |
SearchDropListCommandChevronFillColor | Gets or sets a fill colour for the chevron. The chevron is not drawn with it: use
NormalColorForSearchDropListChevron and
HoverColorForSearchDropListChevron instead. |
SearchDropListCommandInvokerImage | Gets or sets a picture to show in place of the drawn chevron. |
| Methods | |
OnDpiChangedAfterParent | Raises the Control.DpiChangedAfterParent event and resizes and redraws the buttons for
the new display scale. |
OnFontChanged | Raises the Control.FontChanged event, resizes the buttons for the new font, and
re-creates the window so the prompt and the text margin are applied again. |
OnHandleCreated | Raises the Control.HandleCreated event and applies the prompt and the space kept free
for the buttons, both of which belong to the window. |
OnParentChanged | Raises the Control.ParentChanged event, resizes the buttons for the font now inherited,
and re-creates the window. |
OnParentFontChanged | Raises the Control.ParentFontChanged event, resizes the buttons for the new font, and
re-creates the window. |
OnResize | Raises the Control.Resize event and places the three buttons against the right-hand
edge for the new size. |
RefreshControlBlink | Starts or stops the border blinking, to draw the eye to the box. |
| Events | |
ClearSearchTextRequested | Occurs when the user clicks the cross to clear the search. |
SearchDropListOpenRequested | Occurs when the user clicks the chevron, asking for a list – recent searches, saved filters – to be shown. |
TextSearchRequested | Occurs when the user clicks the magnifier to run a search. |
Constructors
SearchTextBox
Initialises a search box showing the prompt "Search..." and all three buttons.
Properties
ClearSearchTextCommandInvokerImage
Gets or sets a picture to show in place of the drawn cross.
- Value
- The picture, or
nullfor the drawn cross. The default isnull.
Remarks
Scaled to fit the button, keeping its proportions, and centred.
CueText
Gets or sets the grey prompt shown while the box is empty – "Search orders", "Find a customer".
- Value
- The prompt. The default is "Search...". An empty string shows no prompt.
Remarks
The prompt is the Windows cue banner, drawn by the system in its own grey; it is never part
of Control.Text.
See also
DefaultDropListChevronBorderColor
Gets or sets the colour of the outline round the drawn chevron.
- Value
- The outline colour. The default is
Color.Teal.
Remarks
No outline is drawn until DefaultDropListChevronBorderWidth is above
zero.
DefaultDropListChevronBorderOnHoverColor
Gets or sets the colour of the chevron's outline while the pointer is over it or its list is open.
- Value
- The outline colour. The default is
Color.Empty, which draws no outline while the chevron is hot.
DefaultDropListChevronBorderWidth
Gets or sets the width of the outline round the drawn chevron.
- Value
- The width in logical pixels. The default is 0: no outline.
See also
DefaultDropListChevronRotateFlipType
Gets or sets which way the drawn chevron points.
- Value
- One of the
RotateFlipTypevalues, applied about the chevron's centre. The default isRotateNoneFlipNone: pointing down.
Remarks
Rotate180FlipNone points it up – for a list that opens above
the box, say.
HoverColorForSearchDropListChevron
Gets or sets the colour of the drawn chevron while the pointer is over it or its list is open.
- Value
- The hover colour. The default is
Color.Tomato. - Designer category
- Colors
See also
IsCueTextRenderedWhenInFocus
Gets or sets whether the prompt stays visible in the empty box while it has the focus.
- Value
trueto keep the prompt until the user types;falseto hide it as soon as the box takes the focus. The default istrue.
IsOnlyWatermarkProvider
Gets or sets whether the box is a plain text box with a prompt, without its three buttons.
- Value
trueto remove the magnifier, the cross and the chevron;falseto show them. The default isfalse.
Remarks
Setting it to false puts all three buttons back, even if
IsSearchCommandSetEnabled is false. While it is
true, changes to IsSearchCommandSetEnabled are ignored.
IsSearchCommandSetEnabled
Gets or sets whether the magnifier and the cross are shown; the chevron is shown either way.
- Value
truefor all three buttons;falseto keep only the chevron, for a box whose list does something other than search. The default istrue.
Remarks
Ignored while IsOnlyWatermarkProvider is true.
IsSearchDropListOpenRequestCompleted
Gets or sets whether the list opened from the chevron has been dealt with – closed or dismissed.
- Value
truewhen no list is open;falsewhile one is. The default istrue.
Remarks
While it is false the chevron stays in
HoverColorForSearchDropListChevron, even after the pointer leaves it. The control sets
it to false when it raises SearchDropListOpenRequested; set it to
true when the list closes, for example in the menu's Closed event.
MarginBetweenTextAndCommandInvokers
Gets or sets the space left between the end of the text and the magnifier.
- Value
- The space in logical pixels. The default is 9.
Remarks
Used while the magnifier and the cross are shown; with only the chevron the text runs up to it.
Multiline
Gets whether the box takes more than one line of text – which a search box never does.
- Value
- Always
false.
NormalColorForSearchDropListChevron
Gets or sets the colour the drawn magnifier, cross and chevron are drawn in.
- Value
- The glyph colour. The default is
Color.SteelBlue. - Designer category
- Colors
Remarks
Despite the name, the colour is used for all three drawn buttons. While the box is disabled
they are drawn in SystemColors.ControlDark.
See also
ScrollBars
Gets which scroll bars the box shows – none, as it holds a single line.
- Value
- Always
None.
SearchCommandInvokerImage
Gets or sets a picture to show in place of the drawn magnifier.
- Value
- The picture, or
nullfor the drawn magnifier. The default isnull.
Remarks
The picture is scaled to fit the button, keeping its proportions, and centred. A picture with transparency, 32 x 32 or larger, looks best at high display scales.
SearchDropListCommandChevronFillColor
Gets or sets a fill colour for the chevron. The chevron is not drawn with it: use
NormalColorForSearchDropListChevron and
HoverColorForSearchDropListChevron instead.
- Value
- The stored colour. The default is
Color.Empty. - Designer category
- Colors
SearchDropListCommandInvokerImage
Gets or sets a picture to show in place of the drawn chevron.
- Value
- The picture, or
nullfor the drawn chevron. The default isnull.
Remarks
Scaled to fit the button, keeping its proportions, and centred. A picture does not change
colour under the pointer, and DefaultDropListChevronRotateFlipType does not apply to
it.
Methods
OnDpiChangedAfterParent
Raises the Control.DpiChangedAfterParent event and resizes and redraws the buttons for
the new display scale.
Parameters
e- The data for the event.
OnFontChanged
Raises the Control.FontChanged event, resizes the buttons for the new font, and
re-creates the window so the prompt and the text margin are applied again.
Parameters
e- The data for the event.
OnHandleCreated
Raises the Control.HandleCreated event and applies the prompt and the space kept free
for the buttons, both of which belong to the window.
Parameters
e- The data for the event.
OnParentChanged
Raises the Control.ParentChanged event, resizes the buttons for the font now inherited,
and re-creates the window.
Parameters
e- The data for the event.
OnParentFontChanged
Raises the Control.ParentFontChanged event, resizes the buttons for the new font, and
re-creates the window.
Parameters
e- The data for the event.
OnResize
Raises the Control.Resize event and places the three buttons against the right-hand
edge for the new size.
Parameters
e- The data for the event.
RefreshControlBlink
Starts or stops the border blinking, to draw the eye to the box.
Parameters
shouldBlinktrueto start blinking, restarting any blinking already under way;falseto stop it.blinkColor- The colour the border flashes to, every half second. Ignored when
shouldBlinkisfalse.
Remarks
A shorthand for BinaryExtendedText.StopBlinking followed by
BinaryExtendedText.StartToBlink(), with the same conditions.
Example
// No matches: flash the box until the user changes the search.
searchBox.RefreshControlBlink(results.Items.Count == 0, Color.OrangeRed);Events
ClearSearchTextRequested
Occurs when the user clicks the cross to clear the search.
Remarks
The control does not clear its text; do that in the handler – with
Clear – along with resetting whatever the search filtered.
SearchDropListOpenRequested
Occurs when the user clicks the chevron, asking for a list – recent searches, saved filters – to be shown.
Remarks
SearchTextBoxDropListInvokedEventArgs.DropListInvokerTopLeftPoint is the
chevron's top-left corner in the search box's client coordinates; show the list there, or below the
box.
Before raising the event the control sets IsSearchDropListOpenRequestCompleted
to false, which keeps the chevron in its hover colour. Set it back to
true when the list closes. The event is not raised, and the property not changed,
when no handler is attached.
TextSearchRequested
Occurs when the user clicks the magnifier to run a search.
Remarks
SearchTextBoxRequestEventArgs.SearchText holds the box's text at the moment of
the click – possibly empty. Pressing Enter does not raise this event.
Example
searchBox.TextSearchRequested += (sender, e) =>
ordersGrid.DataSource = orderRepository.FindByCustomer(e.SearchText);