Hybrid Edit Control that Combines Prompt Text and Edit Control | CodeGuru

Hybrid Edit Control that Combines Prompt Text and Edit Control

This article was contributed by Tom Archer. Initialized dialog (before user has input any data). Notice prompts inside edit controls that relieve dialog designer of having to place static controls on dialog (or view). Introduction If you’ve ever used Intuit’s Quicken or Peachtree’s Accounting for Windows, you may have noticed something called a “grey edit […]

Written By
CodeGuru Staff
CodeGuru Staff
May 17, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

This article was contributed by
Tom Archer.


Initialized dialog (before user has input any data). Notice prompts
inside edit controls that relieve dialog designer of having to place static controls on
dialog (or view).

Introduction

If you’ve ever used Intuit’s Quicken or Peachtree’s Accounting for Windows, you
may have noticed something called a “grey edit control” on some of the dialogs and views.
As you can see in the figure above, a grey edit control is one in which the text is
grey and italicized until the control has focus or until data has been entered into the
control. Where this little control becomes useful is in situations where there is simply
no room on the dialog (or view) for a static control telling the user what information
is required. An example might be the address portion of a form where
the controls are “nestled” so closely together that any attempt at placing static controls
near the edit controls would look awful. Another great example of where this control is
useful is when you want your dialog (or view)
to (as closely as possible) represent an actual paper document such as an invoice or
purchase order.

Rules for Display


Rules are set so that the user can tell a prompt from “real” data:
  • If the field has a value, then display that value
  • If field has focus and no value, then display a blank
  • If field does not have focus and has no value, show prompt (in italics)
    so that user knows what is needed.

Using the grey edit control, the programmer can have the control’s prompt be
displayed in the edit control itself. As you can see above, the rules that are used for displaying the data are very
simple. The grey and italicized font lets the user know that
what they are looking at is a prompt and not the data itself. After the control receives
focus, the grey text disappears so that the
user is not distracted. When the control loses focus, whether or not the grey
text returns depends on whether the user typed
any information into the control.

Advertisement

Using the CGreyEdit Control

To use the CGreyEdit control, all you need to do is instantiate it and call its
Init method function.

BOOL CGreyEdit::Init(UINT uiControlId,
                     CWnd *pParent,
                     const char*lpszDefaultText,
                     COLORREF lBackgroundColor,
                     UINT uiAlignment)

where

  • uiControlId – Resource ID of the edit control you want to subclass
  • pParent – Parent window of the control
  • lpszDefaultText – Default, or prompt text, to be displayed until control has focus or user has entered data into the control
  • lBackgroundColor – This value defaults to COLOR_WINDOWTEXT and represents the controls background color
  • uiAlignment – Valid values include TA_CENTER, TA_LEFT or TA_RIGHT. Defaults to TA_LEFT

To use the control, smply create a member variable of type CGreyEdit
in your dialog or view class. Then call the object’s Init function. Here’s an
example of doing this:

BOOL CGreyEditTestDlg::OnInitDialog()
{
 //...
 m_editName.Init(IDC_EDT_NAME,
                 this,
                 _T("Last Name, First Name"),
                 RGB(255,0,0));
 //...
}

Downloads

Download demo project – 15 Kb

Download source – 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.