Tag Archives: EM_SETCUEBANNER
Set placeholder text for textbox (cue text)
Setting placeholder text for your textboxes helps the users to identify better what kind of information it’s needed from them.
Instead of handling the focus enter and focus leave events in order to set and remove the placeholder text it is possible to use the Windows SendMessage
function to send a EM_SETCUEBANNER
message to our textbox to do the work for us.
This can be done with two easy steps. First we need to expose the Windows SendMessage function.
1 2 3 | private const int EM_SETCUEBANNER = 0x1501; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam); |
Then simply call the method with the handle of our textbox, EM_SETCUEBANNER’s value and the text we want to set.
1 2 | SendMessage(textBox1.Handle, EM_SETCUEBANNER, 0, "Username"); SendMessage(textBox2.Handle, EM_SETCUEBANNER, 0, "Password"); |
The result will be a textbox with a placeholder text of our choice. The placeholder text will be automatically be removed when the textbox gains focus and will only reappear if the textbox loses focus and has no characters typed in it.
Posted in C#.
Tagged C#, csharp, EM_SETCUEBANNER, textbox cue text, tutorial, winforms