Friday, November 03, 2006
Watermarks seem to be all the rage these days for data entry input fields on websites.  In fact, even Vista seems to be jumping on the band wagon with the Search Box.

MSDN Search Box

Recently, the team and I did some UI work for the major project we've been working on. There are lots of neat Visio mockups we put together -- prototypes, etc.. I would add that the UI in general looks fairly slick, if I may say so myself. However out of all the things we did, we were missing this new cool watermark feature.

One of course could fairly trivially achieve this in Windows Forms by overriding the Paint event, among perhaps several other implementations. The problem of course lies in that this must be done for all controls one would like to display a watermark.

You may be asking yourself "how many could there be?". Well, lets see, of the top of my head I think of the following:

  • TextBox

  • MaskedTextBox

  • ComboBox

  • ListBox (Perhaps?)

Ok, five controls is not that bad right? Well let's consider that if you're using Menu and Toolbars, then we need to add at least 2 more, perhaps more if you do ToolStripControlHosting (see How to: Wrap a Windows Forms Control with ToolStripControlHost ).

  • ToolStripTextBox

  • ToolStripComboBox

Ah, so 7 controls, that implement basically the exact same logic. What about DataGridViews? Those have the TextBox cell. I think by now you get the point.

Of course, I have left out the dependencies you're creating for such a simple feature, in case you already have existing controls and or replacing existing ones.

Solution

My solution, instead of using inheritance and overriding, I thought this was a good case to use the IExtenderProvider . This interface does not directly refer to the pseudo pattern Microsoft has been using as an extensibility point in much of the .NET 2.0 stack.

Instead, the purpose of the IExtenderProvider is to let developers augment existing Components/Controls with additionally properties without using inheritance. This feature essentially adds properties to the design time on your control via the PropertyGrid. One of the most common used extender providers is the ToolTip component.

This then begs the question since this interface adds properties, that still does not solve how to draw when there is no text in the control and use the default behavior otherwise.

For this I decided to use an existing control to perform the rendering by superimposing it over the existing controls, namely the label control. To get the desired look, I simply set the background property to transparent and the forecolor to GrayText. That's all!

Part of using this alternative solution to was to prevent the covered control from knowing any implementation details about the watermark, this is just good object oriented design. The label should subscribe to certain events like when the covered control gets focus, then the label should be hidden. Also, when the covered control is resize this should be taken into account so that perhaps the watermark can be centered.

Additional Notes

I decided to use a separate label for each control that receives a watermark, however the WaterMarkColor property on the WaterMarkProvider sets the color for all. Keep in mind you can have multiple WaterMarkProviders on a given form.

BTW, currently there seems to be a bug with the combo box support. I'll look at this later unless someone takes the initative ;-)

Enjoy!

Download

11/3/2006 10:40:05 AM (Eastern Standard Time, UTC-05:00)  #    Comments [23]  | 
11/27/2006 3:34:13 PM (Eastern Standard Time, UTC-05:00)
Nice implementation Eddy. I have to say that was a simple solution that got the job done with a minimum amount of work, good thinking.
9/7/2007 7:15:39 AM (Eastern Standard Time, UTC-05:00)
The imagery needs to remain true to the brand. The splash screen, the favicon.ico on the sight, the tray icon, the web page, about screen, footer of support emails... everything needs to convey the brand through images. The eye patch is an icon of the product; lose it at your peril... In fact, make embroidered eyepatches with the smiley face...
10/12/2010 2:50:45 AM (Eastern Standard Time, UTC-05:00)
Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.
5/11/2011 4:03:59 PM (Eastern Standard Time, UTC-05:00)
I'm not easily ipmerssed. . . but that's impressing me! :)
5/13/2011 10:35:30 AM (Eastern Standard Time, UTC-05:00)
cpYQeV , [url=http://ksbtezstpoxi.com/]ksbtezstpoxi[/url], [link=http://ixalgjxjednm.com/]ixalgjxjednm[/link], http://sbidodfvapfb.com/
5/16/2011 3:50:15 AM (Eastern Standard Time, UTC-05:00)
new jersey car insurance ojksn health insurance qvek car insurance >:-(((
5/17/2011 9:43:05 PM (Eastern Standard Time, UTC-05:00)
term life insurance 8-(( infinity auto insurance 0292 auto insurance quotes 059 auto insurance rates bwdp
5/18/2011 8:19:13 AM (Eastern Standard Time, UTC-05:00)
auto insurance rates :-((( life insurance 864803 cheap business insurance 8-]
5/20/2011 7:32:01 PM (Eastern Standard Time, UTC-05:00)
car insurance 608 new york car insurance 9352 business insurance 6432
5/22/2011 7:17:52 PM (Eastern Standard Time, UTC-05:00)
car insurance 877912 life insurance quotes 8-OO car insurance quotes 8[
5/23/2011 6:11:30 PM (Eastern Standard Time, UTC-05:00)
ticket cheap geusxs tickets plane plui flights international =-O
5/23/2011 6:53:49 PM (Eastern Standard Time, UTC-05:00)
chip health insurance jpfoi pa health insurance qzmr auto insurance quotes %-]
5/24/2011 9:20:43 PM (Eastern Standard Time, UTC-05:00)
colchicine =DD acomplia >:-[[[
5/26/2011 11:49:53 PM (Eastern Standard Time, UTC-05:00)
tramadol wjqxz zithromax >:((
5/28/2011 9:31:25 PM (Eastern Standard Time, UTC-05:00)
cialis tihqm topamax 313
5/28/2011 11:24:01 PM (Eastern Standard Time, UTC-05:00)
viagra 8D viagra pills fua
5/30/2011 6:46:15 PM (Eastern Standard Time, UTC-05:00)
discount auto insurance gvgoq business insurance costs 261807 car insurance cjnapx
5/31/2011 8:52:05 PM (Eastern Standard Time, UTC-05:00)
ultram 061 nexium 779457
6/3/2011 10:51:17 PM (Eastern Standard Time, UTC-05:00)
prednisone pills 0696 cheap life insurance zgz
6/5/2011 8:21:28 PM (Eastern Standard Time, UTC-05:00)
generic viagra levitra and tadalafil >:) acomplia diet drug purephentermine oiot valtrex >:((
6/5/2011 8:55:01 PM (Eastern Standard Time, UTC-05:00)
tramadol 054662 life insurance quotes 444749
6/6/2011 7:52:18 PM (Eastern Standard Time, UTC-05:00)
propecia gex cymbalta >:O order accutane online 39717
6/8/2011 6:49:18 PM (Eastern Standard Time, UTC-05:00)
Viagra genérica barata wcdl halpa cialis verkosta 8-D
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Theme design by Jelle Druyts