by Allomagobie
It is so lousy for .net form component that did not provide such a watermark functionality for its textbox. I've been trying the watermark function in native windows dll for a while. I ended up with this:
public static class TextboxWatermarkExtension
{
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 watermark)
{
SendMessage(textbox.Handle, EM_SETCUEBANNER, 0, watermark);
}
}
That's all folks :) you can see watermaked text on your textbox. The watermark behaviors like native watermark, which is grayscaled text because we use native function of windows.
I think this is the most simple way to do that, rather than create our own textboxt derrived from textbox.