• Download demo project - 32.9 KB

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:

// Operationspublic:    void Reset();// Attributespublic:    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