Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watermark in System.Windows.Forms.TextBox

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).

like image 643
Kb. Avatar asked Feb 23 '09 16:02

Kb.


People also ask

How do I add a Watermark to a TextBox?

On the Design tab, select Watermark > Custom Watermark. Choose Picture Watermark and select a picture, or choose Text watermark and type your watermark text in the Text box. Click OK.

What is Watermark in TextBox?

Set the TextBoxWatermarkExtender properties TargetControlID=" ", WatermarkText=" ". The "TargetControlID" property specifies the TextBox id, on which you want to set the watermark text, and "WatermarkText" accepts the text as watermark for that TextBox. Code for TextBoxWatermarkExtender.

How do I add a Watermark to a TextBox in WPF?

Go to the xaml code behind and add the Namespace to it, so that we can use the class WatermarkHelper. And add the following two Grids inside the LayoutRoot Grid. And then add TextBlocks and TextBoxes to the Grid. TextBlock is for the Watermark text and TextBox is for user input.

How do I add text to a Windows Form?

Step 1: Create a windows form. Step 2: Drag the TextBox control from the ToolBox and Drop it on the windows form. You can place TextBox anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the TextBox control to set the Text property of the TextBox.


2 Answers

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.

like image 99
Peymankh Avatar answered Oct 03 '22 14:10

Peymankh


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.

Link & Link

like image 28
cgreeno Avatar answered Oct 03 '22 15:10

cgreeno