I was wondering if anyone knew how to add watermark text (e.g. "type here..." in gray writing) to textboxes in visual studio 2012. I've searched the web for a solution to no prevail. Any help would be greatly appreciated. Note: I'm a beginner to C++
-
Download [Cue Provider](http://www.codeproject.com/Articles/27853/CueProvider) and reference that in your C++ project. – Jeremy Thompson Mar 29 '13 at 03:44
-
I downloaded it, I tried to reference it to my C++ project though it couldn't read any of the files. – Daniel Mar 29 '13 at 04:03
-
There are several customized TextBox controls that you can use, http://www.codeproject.com/Articles/319910/Custom-TextBox-with-watermark and http://www.codeproject.com/Articles/27849/WaterMark-TextBox-For-Desktop-Applications-Using-C. – Lex Li Mar 29 '13 at 05:52
-
Future hint: the text is called a "placeholder", not a watermark. – Dai Mar 29 '13 at 06:12
-
possible duplicate of [How do I fill an empty textbox with default text?](http://stackoverflow.com/questions/5178247/how-do-i-fill-an-empty-textbox-with-default-text) and [Watermark TextBox in WinForms](http://stackoverflow.com/q/4902565). This is even easier for you, since no P/Invoke is required in C++/CLI. You can just call the appropriate Win32 APIs directly. – Cody Gray - on strike Mar 29 '13 at 08:13
1 Answers
You can, but you don't need any TextBox customizations; you can use events for that. I'd give you the code but 1) visual studio is giving me problems in c++/cli again and 2) it would make you a bit lazy as well.
So, I'm going to describe you a way it can be done: assign the foreground color of the TextBox
as gray (Color::Gray
, for example) and the text as "type here...". Then subscribe the TextBox
's TextChanged
event, and whenever the text is changed (and different), you should first clear the "type here..." and then change the color to Color::Black
. If the text is none, you change the color back to Color::Gray
and the text to "type here...". Pretty simple huh? Yet if you have doubts in the implementation you can ask me by commenting this answer.

- 378
- 3
- 18
-
@Daniel, I now notice you said you're a beginner in C++/CLI (I am too), but if you know C#, it should be quite straightforward. Events can often be complicated, but there's plenty of material about that online, and what applies to C# also works in C++/CLI (except for lambdas and such higher-order stuff). – JMCF125 Apr 19 '13 at 22:41
-
@Daniel, instead of using the event `TextChanged` you can also use `GotFocus` and `LostFocus`, for a probably simpler logic. – JMCF125 Apr 20 '13 at 11:55