How about you assign ToolTip
to TextBox
and put all the "talk what textbox is for" inside that?
Just drag & drop ToolTip
inside the Form. And then in each TextBox
properties you should have additional entry in Misc section ToolTip
on toolTip1
(or whatever it's name will be if you rename it).
Then when user hovers over TextBox
(Read Only/Disabled TextBox
doesn't seems to display ToolTips) and stops there for 1 second ToolTip should show with proper info.
You can eventually or even better have a Label
next to TextBox
saying what is is, but having a ToolTip
is also a good idea to explain more information to user thru that.
For doing stuff with WaterMark so you don't have to go the long way by setting the events, taking care of SelectAll etc you could do it like this. Create new watermark.cs file and replace it with this code. Make sure you have changed namespace to match your program namespace.
#region
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
#endregion
namespace Watermark {
public static class TextBoxWatermarkExtensionMethod {
private const uint ECM_FIRST = 0x1500;
private const uint EM_SETCUEBANNER = ECM_FIRST + 1;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
public static void SetWatermark(this TextBox textBox, string watermarkText) {
SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermarkText);
}
}
}
internal class WatermarkTextBox : TextBox {
private const uint ECM_FIRST = 0x1500;
private const uint EM_SETCUEBANNER = ECM_FIRST + 1;
private string watermarkText;
public string WatermarkText {
get { return watermarkText; }
set {
watermarkText = value;
SetWatermark(watermarkText);
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
private void SetWatermark(string watermarkText) {
SendMessage(Handle, EM_SETCUEBANNER, 0, watermarkText);
}
}
In your Form you activate it like this:
textBoxYourWhatever.SetWatermark("Text that should display");
It stays there as long the TextBox
is empty. When users gets into TextBox
text disappears. It appears again when TextBox
is cleaned (either by user or automatically). No need for any special events etc.
EDIT:
I've added also internal class WaterMarkTextBox which gives you an option to simply use new WaterMarkTexBox that becomes available in Designer. Then drag and drop it to your designer and use it. It behaves like normal textbox just gives you additional field WaterMarkText.
I still prefer the first method thou. Doesn't make you rebuild your gui again.