Bite my bytes

What I learn by day I blog at night - A blog from Microsoft Consultant working from Ljubljana, Slovenia

  Home :: Contact :: Syndication  
  999 Posts :: 7691 Comments :: 235 Trackbacks

Search

Most popular posts

Categories

My Projects

Archives

Stuff


Copyright © by David Vidmar
 
Contact me!
 
LinkedIn Profile
 
 
 

Phew! It's been a while since I posted some code here! Here goes...

I had a modest request from a part-time-UI-designer of application I'm currently working on. He wanted watermarked input boxes as we sometimes see on the web, except that he wanted them in desktop application.

So instead of something like this:

image

We would have this:

 image

Fair enough, I liked it.

I immediately started thinking about Enter and Leave events and overlaying Label control over TextBox control and I had a good starting point with my old LinkTextBox control.

But, man, I'm glad I googled before I started coding. It turns out, the feature is only SendMessage away.

So here are two implementations:

First one is done with subclassing  and creating WatermarkTextBox.

class WatermarkTextBox : TextBox
{
    private const uint ECM_FIRST = 0x1500;
    private const uint EM_SETCUEBANNER = ECM_FIRST + 1;

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

    private string watermarkText;
    public string WatermarkText
    {
        get { return watermarkText; }
        set
        {
            watermarkText = value;
            SetWatermark(watermarkText);
        }
    }

    private void SetWatermark(string watermarkText)
    {
        SendMessage(this.Handle, EM_SETCUEBANNER, 0, watermarkText);
    }       

}

Other one is a simple Extension Method.

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);
    }

}
Posted on Wednesday, November 05, 2008 11:36 PM | Filed under: Developement |

Feedback

# re: Watermarked TextBox in Windows Forms on .NET 9/8/2009 8:44 AM Peymankh
Well, it's great but it won't work in Windows XP.

# re: Watermarked TextBox in Windows Forms on .NET 1/13/2010 9:46 AM nn
ee

# Works in Windows XP 1/18/2010 8:03 PM 0xfeeddeadbeef
@Peymankh:
It works in Windows XP too. See this MSDN article: http://msdn.microsoft.com/en-us/library/bb761639(VS.85).aspx


# re: Watermarked TextBox in Windows Forms on .NET 1/31/2010 3:34 PM hus
Can you provide code of the WinForm app?

# re: Watermarked TextBox in Windows Forms on .NET 8/23/2011 9:21 AM Manju K
It works well.. Thanking you

# re: Watermarked TextBox in Windows Forms on .NET 11/20/2011 2:52 PM awais
how to use this class in my project / on my text box..
i m new in subclassing

# thank you soooooooooooooooooo MUCH 11/20/2011 4:08 PM awais
thank you so much i got it how to use the subclass and the extention method.

ofcurse the ex. method is far more better than that in terms of code complexity and easy to understand.

today i can say i learent something new.
thank you so much who ever you are.
:))))))))

# re: Watermarked TextBox in Windows Forms on .NET 2/9/2012 7:27 AM Nihar
Is it possible on Compact Framework. I know I have to use coredll.dll instead of user32.dll, but I do not know how. I have been trying this for sometime.

Comments have been closed on this topic.