HypernovaDesktop Components · docs
Binarymission
WinForms › Watermark Text Box

Watermark Text Box

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

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".

4public types
39public members
1namespaces

Assembly

AssemblyBinarymission.WinForms.Controls.WatermarkTextBox.dll
Root namespaceBinarymission.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 WatermarkProvider that draws the prompt. A click anywhere on the prompt hides it and puts the caret in the text box.
  • Text is the text the user typed; it follows the inner text box as the user types, and Control.TextChanged is raised for each change.
  • The prompt is shown when IsWatermarkEnabled is true, Text is 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.

Important

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 IWatermarkProviderThe settings a watermark surface exposes: the prompt text and an optional picture beside it.
class WatermarkControlProviderA UserControl that implements IWatermarkProvider with plain, overridable properties - the base of WatermarkProvider.
class WatermarkProviderThe control that draws a WatermarkTextBox's prompt: an optional picture on the left and the prompt text beside it, centred vertically.
class WatermarkTextBoxA 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".