Watermark in System.Windows.Forms.TextBox
Solution 1
lately I needed a watermark textbox, the first thing that popped in to my head was OnLeave and OnEnter events of textbox, but first I googled it and I got two links first was the one in CodeProject which used the System.Drawing namespace and the other one was here using the SendMessage() over here http://vidmar.net/weblog/archive/2008/11/05/watermarked-textbox-in-windows-forms-on-.net.aspx.
I beleive the SendMessage one is much easier and it also has no flickering in it. though I used it.
I hope it will be helpful for you.
Solution 2
It is not as straightforward as one would think. You need to use using the System.Drawing namespace and override the OnPaint event.
Here are some links of some people that have already done it.
Kb.
Currently trying to migrate complex scheduling data using C#, Teams and SQL with Dapper
Updated on February 15, 2020Comments
-
Kb. over 4 years
What is he best way to implement Watermark functionality for a
System.Windows.Forms.TextBox
in .Net 2.0 with C#?
Edit:
Using the ready-made component from CodeProject was very easy. It's also with a The Code Project Open License (CPOL).
-
Kb. about 15 years@BtBh: Thanks. The first link was what I needed. +1 I agree that it is not as straight forward as you would think.
-
Kb. over 14 years@Peymankh: Thanks +1 and green checkmark. SendMessage is much easier
-
John M about 14 yearsAdditional notes on Vidmar usage: setting watermark = this.txtBoxA.SetWatermark("BOX"); * *references needed = using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices;
-
User13839404 about 13 yearsHow to use it in a windows application?
-
Lee Richardson over 11 yearsTried this approach (EM_SETCUEBANNER) and the CodeProject one. The only disadvantage to EM_SETCUEBANNER is you can't set the watermark color, otherwise it rocks. The disadvantage to the codeproject approach is the flicker Peymankh mentioned and the watermark text doesn't disappear until you start typing (not on focus).
-
Igor Meszaros about 10 yearsI would recommend this as an answer!
-
Andy almost 9 yearsI created a small GitHub repository showing all methods to create a watermark. github.com/akorb/PlaceholderTextBox
-
Armand Bernard almost 3 yearsWhile I like this solution, I'd argue that since it is rather short and the link may die in the future, the code should be included in the answer body. Please do keep linking the original source for credit though.