Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

XEditPrompt - CEdit-derived control with web-like prompt

0.00/5 (No votes)
20 Jul 2007 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here