Watermark Text Box
A text box that shows a prompt – a line of grey or coloured text, with an optional picture – while it is empty, telling the user what to type: "Search orders", "Customer reference", "Notes for the courier".
Assembly
| Assembly | Binarymission.WinForms.Controls.WatermarkTextBox.dll |
| Root namespace | Binarymission.WinForms.Controls.InputControls |
| Target frameworks | .NET Framework 4.8, .NET 6 / 8 / 10 |
Overview
What it is for
Use it in place of a TextBox wherever a label beside the box
would take room the form does not have, or where a hint of the expected input helps more than a
caption. The prompt – the watermark – disappears as soon as the user clicks into the box or the box
holds text, and comes back when the box is left empty.
How it works
- The control hosts an ordinary multi-line text box and, laid over it, a
WatermarkProviderthat draws the prompt. A click anywhere on the prompt hides it and puts the caret in the text box. Textis the text the user typed; it follows the inner text box as the user types, andControl.TextChangedis raised for each change.- The prompt is shown when
IsWatermarkEnabledistrue,Textis empty and the control does not have the focus.
Appearance
WatermarkText, WatermarkTextFont,
WatermarkTextForeColor and WatermarkBackColor style the prompt;
WatermarkImage with ShouldRenderWatermarkImage adds a picture to the left
of it. The control draws a one-pixel border in BorderColor round the box.
The control clears Text when its window is created, so text
assigned before the form is shown is discarded. Set initial text once the form is on screen – in the
form's Form.Shown event, for example.
Getting started
using System;
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.InputControls;
public class CourierNotesForm : Form
{
private readonly WatermarkTextBox notesBox;
private readonly Label characterCount;
public CourierNotesForm()
{
Text = "Delivery instructions";
ClientSize = new Size(420, 180);
notesBox = new WatermarkTextBox
{
Location = new Point(12, 12),
Size = new Size(396, 120),
WatermarkText = "Notes for the courier - gate codes, safe places, opening hours",
WatermarkTextForeColor = SystemColors.GrayText,
WatermarkTextFont = new Font("Segoe UI", 9f, FontStyle.Italic),
BorderColor = SystemColors.ControlDark,
TextScrollBars = ScrollBars.Vertical
};
notesBox.TextChanged += (sender, e) =>
characterCount.Text = notesBox.Text.Length + " / 500 characters";
characterCount = new Label { Location = new Point(12, 142), AutoSize = true };
Controls.Add(notesBox);
Controls.Add(characterCount);
}
}API reference
Binarymission.WinForms.Controls.InputControls
interface IWatermarkProvider | The settings a watermark surface exposes: the prompt text and an optional picture beside it. |
class WatermarkControlProvider | A UserControl that implements IWatermarkProvider with plain, overridable properties - the base of WatermarkProvider. |
class WatermarkProvider | The control that draws a WatermarkTextBox's prompt: an optional picture on the left and the prompt text beside it, centred vertically. |
class WatermarkTextBox | A text box that shows a prompt - a line of grey or coloured text, with an optional picture - while it is empty, telling the user what to type: "Search orders", "Customer reference", "Notes for the courier". |