13

Watermark textbox in .NET Forms

by Allomagobie

It is so lousy for .net form component that did not provide such a watermark functionality for its textbox. I've been trying the watermark function in native windows dll for a while. I ended up with this:

public static class TextboxWatermarkExtension
    {
        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 watermark)
        {
            SendMessage(textbox.Handle, EM_SETCUEBANNER, 0, watermark);
        }
    }

That's all folks :) you can see watermaked text on your textbox. The watermark behaviors like native watermark, which is grayscaled text because we use native function of windows.

I think this is the most simple way to do that, rather than create our own textboxt derrived from textbox.

Tags: , ,

Programmings

Comments are closed

Copyright © 2009 PT. Allomagobie Indonesia. Email: info@allomagobie.com
Apartemen City Resort, Tower Marigold Lantai Dasar No.3, Jl. Kamal Raya Outer Ring Road, Cengkareng - Jakarta Barat 11730

Designed by Allomagobie Team. Powered by BlogEngine.NET 1.5.0.7