How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Set placeholder text for textbox (cue text)

Setting placeholder text for your textboxes helps the users to identify better what kind of information it’s needed from them.

Instead of handling the focus enter and focus leave events in order to set and remove the placeholder text it is possible to use the Windows SendMessage function to send a EM_SETCUEBANNER message to our textbox to do the work for us.

This can be done with two easy steps. First we need to expose the Windows SendMessage function.

Then simply call the method with the handle of our textbox, EM_SETCUEBANNER’s value and the text we want to set.

The result will be a textbox with a placeholder text of our choice. The placeholder text will be automatically be removed when the textbox gains focus and will only reappear if the textbox loses focus and has no characters typed in it.

Cue text example

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

13 Responses to Set placeholder text for textbox (cue text)

  1. Dave says:

    Is there an answer for combo boxes?

  2. Jesus says:

    necesitan declarar

    using System.Runtime.InteropServices;

  3. Umair Mahmood says:

    When i put this code in my application, compiler does not give any error But It is not giving me expected output… Very Sad….

  4. Niel says:

    thank you so much! :)

  5. MS says:

    Thankx for your effort, can I use this same for “ComboBox” also?

  6. MAHMOUD says:

    THIS CODE CANT WORKING IN MY PROJECT PLEASE I NEED EXAMPLE PLACE HOLDER IN TEXTBOX C# WINFORMS

  7. Tobias Vandenbempt says:

    Styling of Cue Text also not possible?

  8. Naxiz says:

    This was exactly what I was looking for! Very easy. Too bad it doesn’t work with multiline textboxes.

Leave a Reply

Your email address will not be published. Required fields are marked *