I read some articles with regards on watermarks in windows app, but I found it to be quite complicated. What is the best way to implement watermark in textboxes?

Add this:

public static class TextBoxWatermarkExtensionMethod {
    private const uint ECM_FIRST = 0x1500;
    private const uint EM_SETCUEBANNER = ECM_FIRST + 1;

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

    public static void SetWatermark(this TextBox textBox, string watermarkText) {
        SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermarkText);
    }
}

After that you can do this:

myTextbox.SetWatermark("This is the watermark");
commented: thanks +1

Thanks. That's easy compared to sub classing it.

Hello, i'm sorry if this is late but where could I call the watermark class for my textbox? Tried in my form's load event but it's not present.

If it isn't present then you didn't include the above code correctly.

I copied the code and put it in code behind. How to include it correctly?

That code works for winforms applications, not ASP.NET. To watermark on ASP you'll have to use javascript, if it's possible.

I'm using it in winforms application.

I've fixed it! Thanks!

I've fixed it! Thanks!

Just wanted to say 'code behind' is an ASP.NET term :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.