Click here to Skip to main content
13,662,167 members
Click here to Skip to main content
Add your own
alternative version

Stats

116.8K views
11K downloads
53 bookmarked
Posted 7 May 2013
Licenced CPOL

TextBox with Placeholder

, 2 May 2018
Rate this:
Please Sign up or sign in to vote.
Extended TextBox with placeholder

Introduction

In many cases, a TextBox with an easy Placeholder property would be very helpful especially when you want the user to filter a ListView or ListBox by search terms.

So, I never found a really good one. I know there exists a very easy method with only one SendMessage method, but I don't like it because the placeholder is hard to differentiate with the actually typed text and I don't like these message things very much, too many things can go wrong.

Now I created my own control and my destination is that it is very easy to use.

Using the Code

Add the PlaceholderTextBox.cs to your project and build it. You can add it with the designer now.

The difference to the usual TextBox is it that there are three new properties:

  • PlaceholderText: It's not hard to guess what this property does. It defines the Placeholder text which will get displayed if the TextBox is empty.
  • IsPlaceholderActive: Through this property, you can always check if the textbox shows the placeholder or real text at the moment.

PlaceholderText is accessible with the designer, IsPlaceholderActive is not!

Besides, there is the new method Reset(). This will return the TextBox to the start state.

The Text property is not accessible with the designer. That's to keep the standard. The placeholder should always be seen before you type any text in the TextBox.

Points of Interest

It's a very well written code. I recommend you read it, there are many comments and maybe you have a suggestion.

I hope you like my control and when you have any suggestions or have found a bug, please let me know!

History

v1.6

  • "Select all" in Windows default context menu does not select the placeholder anymore
  • TextLength returns 0 if placeholder is active
  • Minor code improvements

v1.5

  • Fixed behavior with pressing Ctrl+A while placeholder is active
  • MaxLength working now
  • Fixed crashes if setting Text programmatically

v1.4

v1.3

  • Fixed bug: Font can be changed now - Thanks to Member 11133660
  • Improved internal logic
  • Removed IsItalics
  • Renamed PlaceholderInsideChanged to PlaceholderActiveChanged

v1.2

  • Improved internal logic
  • Removed sealed
  • Added Placeholder category to designer for better overview
  • Fixed some comments

v1.1

  • Added PlaceholderInsideChanged event
  • Fixed bug that new MouseDownEvents never occur
  • Prevents user to go through placeholder with arrow keys

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andiiiii
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionPlaceholder / prompt text? Pin
RenniePet1-Jun-18 16:09
memberRenniePet1-Jun-18 16:09 
QuestionActs the same as MFC's CEdit::SetCueBanner( )? Pin
Member 105465763-May-18 20:09
memberMember 105465763-May-18 20:09 
AnswerRe: Acts the same as MFC's CEdit::SetCueBanner( )? Pin
Andiiiii4-May-18 23:42
memberAndiiiii4-May-18 23:42 
GeneralRe: Acts the same as MFC's CEdit::SetCueBanner( )? Pin
Member 105465766-May-18 21:09
memberMember 105465766-May-18 21:09 
BugNot really a bug but... Pin
yarecky28-Apr-18 23:31
memberyarecky28-Apr-18 23:31 
GeneralRe: Not really a bug but... Pin
chrisbray30-Apr-18 3:56
memberchrisbray30-Apr-18 3:56 
GeneralRe: Not really a bug but... Pin
chrisbray30-Apr-18 4:17
memberchrisbray30-Apr-18 4:17 
GeneralRe: Not really a bug but... Pin
chrisbray30-Apr-18 5:43
memberchrisbray30-Apr-18 5:43 
PraiseRe: Not really a bug but... Pin
yarecky30-Apr-18 5:50
memberyarecky30-Apr-18 5:50 
GeneralRe: Not really a bug but... Pin
chrisbray30-Apr-18 6:06
memberchrisbray30-Apr-18 6:06 
GeneralRe: Not really a bug but... Pin
yarecky30-Apr-18 8:50
memberyarecky30-Apr-18 8:50 
GeneralRe: Not really a bug but... Pin
AndyFTW2-May-18 5:36
memberAndyFTW2-May-18 5:36 
Questionlost MaxLength feature Pin
tao841-Mar-18 16:18
membertao841-Mar-18 16:18 
AnswerRe: lost MaxLength feature Pin
chrisbray30-Apr-18 6:02
memberchrisbray30-Apr-18 6:02 
GeneralRe: lost MaxLength feature Pin
AndyFTW2-May-18 3:45
memberAndyFTW2-May-18 3:45 
QuestionNice, but ... Pin
Alex (RSA)16-Feb-18 0:16
professionalAlex (RSA)16-Feb-18 0:16 
QuestionDemo is not Downloading Pin
Member 1320815325-Aug-17 20:40
memberMember 1320815325-Aug-17 20:40 
BugBug 3 - fails if CharacterCasing set to Upper or Lower, but Place Holder Text is in another case. Pin
VBNetHack2-Jul-17 3:52
memberVBNetHack2-Jul-17 3:52 
GeneralRe: Bug 3 - fails if CharacterCasing set to Upper or Lower, but Place Holder Text is in another case. Pin
AndyFTW15-Feb-18 5:08
memberAndyFTW15-Feb-18 5:08 
Questionbug 2 Pin
Member 1241201717-Jun-17 9:44
memberMember 1241201717-Jun-17 9:44 
AnswerRe: bug 2 Pin
AndyFTW15-Feb-18 5:07
memberAndyFTW15-Feb-18 5:07 
Questionbug Pin
Member 1241201717-Jun-17 0:47
memberMember 1241201717-Jun-17 0:47 
QuestionOther Objects... like MaskedEditBox and Combobox. Pin
Eagles_Power24-Jan-17 2:45
memberEagles_Power24-Jan-17 2:45 
GeneralMy vote of 5 Pin
Maxwolf Goodliffe12-Dec-14 7:08
memberMaxwolf Goodliffe12-Dec-14 7:08 
QuestionDownload demo project link is broken Pin
Member 1125706523-Nov-14 3:26
memberMember 1125706523-Nov-14 3:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 2 May 2018
Article Copyright 2013 by Andiiiii
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid