Sei sulla pagina 1di 5

4/29/2018 TextBox with Placeholder - CodeProject

13,516,944 members Sign in

Search for articles, questions, tips


articles Q&A forums lounge

TextBox with Placeholder


AndyFTW, 28 Apr 2018

   4.09 (28 votes) Rate this:

Extended TextBox with placeholder

Download source code - 3 KB


Download demo - 14.2 KB

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.

https://www.codeproject.com/Tips/589115/TextBox-with-Placeholder 1/5
4/29/2018 TextBox with Placeholder - CodeProject

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.5
Fixed behavior with pressing Ctrl+A while placeholder is active
MaxLength working now
Fixed crashes if setting Text programmatically

v1.4
Fixed bugs reported by Member 12412017
Fixed bug reported by Member 13208153

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
https://www.codeproject.com/Tips/589115/TextBox-with-Placeholder 2/5
4/29/2018 TextBox with Placeholder - CodeProject

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

Share
TWITTER FACEBOOK

About the Author


AndyFTWNo Biography provided
G
e
r
m
a
n
y

You may also be interested in...


Microsoft Content Management Server Custom Beyond Arduino Create: Developing UP Squared
Properties Placeholder Projects in Intel® System Studio

MCMS Placeholder Content Web Part Wind River Helix Device Cloud Application
Deployment: POC Retail Vending Machine

Building a New Parse Server & MongoDB Atlas- Angular 5 – Reactive Forms with Dynamic
Based Application FormArray and Simple Validation

Comments and Discussions


 

You must Sign In to use this message board.

https://www.codeproject.com/Tips/589115/TextBox-with-Placeholder 3/5
4/29/2018 TextBox with Placeholder - CodeProject

Search Comments

First Prev Next

Not really a bug but...


yarecky 3hrs 25mins ago

lost MaxLength feature


tao84 1-Mar-18 16:18

Nice, but ...


Alex (RSA) 16-Feb-18 0:16

Demo is not Downloading


Member 13208153 25-Aug-17 20:40

Bug 3 - fails if CharacterCasing set to Upper or Lower, but Place Holder Text is in another case.
VBNetHack 2-Jul-17 3:52

Re: Bug 3 - fails if CharacterCasing set to Upper or Lower, but Place Holder Text is in another case.
AndyFTW 15-Feb-18 5:08

bug 2
Member 12412017 17-Jun-17 9:44

Re: bug 2
AndyFTW 15-Feb-18 5:07

bug
Member 12412017 17-Jun-17 0:47

Other Objects... like MaskedEditBox and Combobox.


Eagles_Power 24-Jan-17 2:45

My vote of 5
Maxwolf Goodliffe 12-Dec-14 7:08

Download demo project link is broken


Member 11257065 23-Nov-14 3:26

Custom Font is not applied


Member 11133660 6-Oct-14 12:55

Re: Custom Font is not applied


AndyFTW 7-Oct-14 3:25

Please show and explain the code


PIEBALDconsult 4-Sep-13 18:38

[My vote of 1] Win Form now?


David Furshpan 4-Sep-13 13:30

Re: [My vote of 1] Win Form now?


AndyFTW 6-Sep-13 2:23

https://www.codeproject.com/Tips/589115/TextBox-with-Placeholder 4/5
4/29/2018 TextBox with Placeholder - CodeProject

Re: [My vote of 1] Win Form now?


FernandoUY 6-Sep-13 8:08

My vote of 5
Meng FanRui 3-Sep-13 23:51

My vote of 2
FernandoUY 2-Sep-13 14:09

Re: My vote of 2
AndyFTW 6-Sep-13 2:22

Re: My vote of 2
FernandoUY 6-Sep-13 8:05

Not an article
OriginalGriff 16-May-13 2:29

Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin   

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

Permalink | Advertise | Privacy | Terms of Use | Mobile Article Copyright 2013 by AndyFTW
Select Language ▼
Web04-2016 | 2.8.180417.1 | Last Updated 29 Apr 2018 Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid

https://www.codeproject.com/Tips/589115/TextBox-with-Placeholder 5/5

Potrebbero piacerti anche