Click here to Skip to main content
15,881,027 members
Articles / Desktop Programming / MFC
Article

XEditPrompt - CEdit-derived control with web-like prompt

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
20 Jul 2007CPOL2 min read 81.4K   2.2K   65   12
XEditPrompt implements an edit control with built-in prompt and text color, just like you see on web pages.

Introduction

The XEditPrompt control is based on CEdit and mimics effect seen on web pages, where the text input field will have some initial text (like <Enter text here>), that will disappear as soon as the user clicks on that field.

How It Works

Initially XEditPrompt will display predefined prompt string, using predefined prompt color (both prompt string and color may be changed programmatically):

screenshot

When user action causes XEditPrompt to gain focus, the initial prompt string is removed:

screenshot

The standard system colors such as COLOR_GRAYTEXT and COLOR_3DDKSHADOW are very light, so I chose RAL color for prompt text:

screenshot

Implementation Details

The API for XEditPrompt is very simple, since its only properties are prompt text and color, plus function to reset state of the control:

C++
// Operations
public:
    void Reset();

// Attributes
public:
    COLORREF  GetPromptColor()   { return m_crPromptColor; }
    CString   GetPromptText()    { return m_strPromptText; }
    void      SetPromptColor(COLORREF crText);
    void      SetPromptText(LPCTSTR lpszPrompt);

How To Use

To integrate CXEditPrompt class into your app, you first need to add the following files to your project:

  • XEditPrompt.cpp
  • XEditPrompt.h

Next, include header file XEditPrompt.h in the appropriate project files (usually, this will be in the header file for dialog class). Then replace declaration of CEdit control with this:

C++
CXEditPrompt   m_MyEdit;

(use whatever variable name already exists).

Now you are ready to start using CXEditPrompt. In dialog's OnInitDialog() function, insert line:

C++
m_MyEdit.SetPromptText(_T("<This is my special prompt>"));

Other Implementations

  • Prompting user for values from a CEdit by Tanzim Husain.
  • SetCueBanner() in Win2000, XP, and Vista allows you to set a prompt for a standard CEdit. The main differences between SetCueBanner() and my implementation is that it has a persistent prompt that keeps popping up until you actually enter some text, and the color of the prompt text cannot be changed. SetCueBanner() is available in Win2000 and later. According to MSDN, it is unsupported in Win98/ME.

Revision History

Version 1.1 — 2007 July 21

  • Changed tabbing behavior. Some people have said to me that just tabbing to an edit box should not cause the prompt to be removed - only direct action by user, like mouse click or keypress, since you may be hitting tab several times to get to another input control, and along the way erase the edit prompt. In this release, the edit prompt will no longer be removed by tabbing.
  • Added example of SetCueBanner() to the demo app.

Version 1.0 — 2007 July 5

  • Initial public release

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

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


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
GeneralSuggest adding GetWindowTextLength Pin
Graham Shanks23-Nov-08 12:00
Graham Shanks23-Nov-08 12:00 
GeneralHandling disabled and read only states [modified] Pin
Graham Shanks6-Nov-08 6:10
Graham Shanks6-Nov-08 6:10 
GeneralRegaining the prompt Pin
Graham Shanks27-Oct-08 6:37
Graham Shanks27-Oct-08 6:37 
GeneralRe: Regaining the prompt Pin
molsson8414-Apr-11 1:19
molsson8414-Apr-11 1:19 
AnswerRe: Regaining the prompt Pin
Hans Dietrich14-Apr-11 1:33
mentorHans Dietrich14-Apr-11 1:33 
GeneralSetCueBanner can't work Pin
banbanyy14-May-08 18:02
banbanyy14-May-08 18:02 
GeneralSetCueBanner() Pin
Ravi Bhavnani6-Jul-07 3:05
professionalRavi Bhavnani6-Jul-07 3:05 
GeneralRe: SetCueBanner() Pin
sps-itsec466-Jul-07 4:45
sps-itsec466-Jul-07 4:45 
GeneralRe: SetCueBanner() Pin
Nacereddine10-Jan-09 3:49
professionalNacereddine10-Jan-09 3:49 
GeneralLooks great... one suggestion Pin
sps-itsec465-Jul-07 22:27
sps-itsec465-Jul-07 22:27 
GeneralRe: Looks great... one suggestion Pin
Hans Dietrich6-Jul-07 0:13
mentorHans Dietrich6-Jul-07 0:13 
GeneralVery nice Pin
Bhaskar Priya5-Jul-07 21:06
Bhaskar Priya5-Jul-07 21:06 

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.