Click here to Skip to main content
Click here to Skip to main content
Go to top

XEditPrompt - CEdit-derived control with web-like prompt

, 20 Jul 2007
Rate this:
Please Sign up or sign in to vote.
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:

// 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:

    CXEditPrompt   m_MyEdit;

(use whatever variable name already exists).

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

    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)

Share

About the Author

Hans Dietrich
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 PinmemberGraham Shanks23-Nov-08 12:00 
GeneralHandling disabled and read only states [modified] PinmemberGraham Shanks6-Nov-08 6:10 
GeneralRegaining the prompt PinmemberGraham Shanks27-Oct-08 6:37 
GeneralRe: Regaining the prompt Pinmembermolsson8414-Apr-11 1:19 
AnswerRe: Regaining the prompt PinmentorHans Dietrich14-Apr-11 1:33 
GeneralSetCueBanner can't work Pinmemberbanbanyy14-May-08 18:02 
GeneralSetCueBanner() PinmemberRavi Bhavnani6-Jul-07 3:05 
GeneralRe: SetCueBanner() Pinmembermykel6-Jul-07 4:45 
GeneralRe: SetCueBanner() PinmemberNacereddine10-Jan-09 3:49 
GeneralLooks great... one suggestion Pinmembermykel5-Jul-07 22:27 
GeneralRe: Looks great... one suggestion PinmvpHans Dietrich6-Jul-07 0:13 
GeneralVery nice PinmemberBhaskarPriya5-Jul-07 21:06 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 21 Jul 2007
Article Copyright 2007 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid