This question already has an answer here:
- Watermark TextBox in WinForms 7 answers
I'm currently making a Windows Forms Application on Visual Studio in C# and I'm trying to find a way to have a real hint.
I've found a lot of answers online on how to have some text preset there, Some examples even show how to grey out the text to look like a placeholder, but that's not what I'm looking for.
I want a grayed out text that you don't have to backspace to type something there. So I want it to behave like an HTML placeholder like the "Search Q&A" search bar on stack Overflow.
Is there an easy way to do this, like configuring a property of the textbox in the designer on Visual Studio?
I know that this is an old question; However I was searching for a way and I found my answer to be best answer to display a prompt text in a
TextBox
:1) Create a class
.cs
file called for exampleMyExtensions.cs
having a namespace called for example 'Extensions'.2) Create a method in the
TextBox
called Init(string prompt) that takes the prompt text you want to display inside theTextBox
.3) Let me stop talking and give you the rest of the code for
MyExtensions.cs
(The entire code):MyExtensions.cs
Now Assume that you have three
TextBox
's :tbUsername
,tbPassword
,tbConfirm
:In your Form_Load(object sender, EventArgs e) method initialize your three TextBox's to have their appropriate Prompt Text Messages:
Enjoy! :)
What about this
the this.ActiveControl = label1; is just to take focus away from the text box initially. If something else already does, don't worry about that line.
This might be the ugliest code but I think you can improve it.
This following class is merely an extension of the standard TextBox
Copy/paste to new cs file entitled PHTextBox.cs.
Go to your graphic designer and add a TextBox. Go to the designer and change the instiantion line for the textbox as follow:
Now compile but before you do, just make sure the textbox is not the first element to get the focus. Add button for that matter.
Please, take a look at my ControlHintManager class, ControlHintInfo type and ControlHintType enumeration. They are written in Vb.Net, but you can get the idea analyzing the source-code, or compile the library to use it in your C# project without any more effort.
My solution has even better behavior than the StackOverflows hint that you mentioned, taking into account that when the control leave focus, the hint-text should be restored if the string remains empty.
Usage is so friendlly:
To acchieve this by your own, one way is calling the Win32
SendMessage
function with the EM_SETCUEBANNER
message, however, that will produce a too basic hint with poor behavior, not recommendable,so a proper way to acchieve this is by taking control of the edit-control text by yourself, handling the
Control.HandleCreated
,Control.Enter
,Control.Leave
,Control.MouseDown
,Control.KeyDown
andControl.Disposed
events (as you can see in my linked source-code).Just use a object to keep track of the control's state (forecolor, text, and optionally the font), then use properlly the event-handlers mentioned to set or restore the text and color.
This is a online C# translation of the most-important code of the linked urls if this help you understand it better:
PS: It is Reflection based to support more variety of controls.
Have you tried overlapping a label on the textbox?
On the textbox keypress event you can check the length of the textbox.text and set the label.
On the keypress event..
Of course you might want to set the default text of the label as well as grey it out too.
Issue with this is if your form is re sizable.
What you want to achieve is not native to windows forms.