Displaying "Type here to ..." until the user enters text into a TextBox
is a well-known usability feature nowadays. How would one implement this feature in C#?
My idea is to override OnTextChanged
, but the logic to handle the changes of text from and to "Type here" is a bit tricky...
Displaying "Type here" on initialization and removing it on first input is easy, but I want to display the message every time the entered text becomes empty.
Handle the lost focus event and if the property Text is empty, fill it with your default string.
In the last version of C# the TextBox has the property PlaceholderText, which does all work. So you only need to set "Type here..." as value of this property.
Set textbox.text as "Type here to ..."
create an event, say box_click()
-->Put this code in your method
now assign this method to the "Enter" event of your textbox(maybe one or many)
PRODUCES SIMILAR OUTPUT TO HTML WATERMARK
Here is my code for textbox "watermark" or "preview" text - works great! Using Windows Forms Application.
NOTE: This example has 3 text boxes, each has the below method for the "mouse leave" event, and "mouse enter" event respectively.