Something that has worked for me:
this.waterMarkActive = true;
this.textBox.ForeColor = Color.Gray;
this.textBox.Text = "Type here";
this.textBox.GotFocus += (source, e) =>
{
if (this.waterMarkActive)
{
this.waterMarkActive = false;
this.textBox.Text = "";
this.textBox.ForeColor = Color.Black;
}
};
this.textBox.LostFocus += (source, e) =>
{
if (!this.waterMarkActive && string.IsNullOrEmpty(this.textBox.Text))
{
this.waterMarkActive = true;
this.textBox.Text = "Type here";
this.textBox.ForeColor = Color.Gray;
}
};
Where bool waterMarkActive
is a class member variable and textBox
is the TextBox
. This probably should be encapsulated though :) There might be some issues with this approach, but I'm not currently aware of any.
I recently discovered that Windows support water marks in text boxes; they are called cue banners (see here). It's very easy to implement:
// Within your class or scoped in a more appropriate location:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
// In your constructor or somewhere more suitable:
SendMessage(textBox.Handle, 0x1501, 1, "Please type here.");
Where textBox
is an instance of TextBox
, 0x1501
is the code for the windows message EM_SETCUEBANNER
, the wParam
may either be TRUE
(non-zero) or FALSE
(zero), and lParam
is the water mark you'd like to display. wParam
indicates when the cue banner should be displayed; if set to TRUE
then the cue banner will be displayed even when the control has focus.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…