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).


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.

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)
  • 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()
                 _T("Last Name, First Name"),


Download demo project - 15 Kb
Download source - 3 Kb

About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.


  • Exactly what I needed, but...

    Posted by Legacy on 07/17/2002 12:00am

    Originally posted by: Uwe

    Hi Tom,

    this code is really great, thank you!!!

    But the background and text colors don't change (only on WinNT???). The text is always black, the background is always white. The OnCtlColor doesn't get called to change the colors. How can I fix this problem?


  • ExEditors Library

    Posted by Legacy on 03/19/2002 12:00am

    Originally posted by: Mike Philips

    A complete collection of editors in a single file:



  • More controls like this if people are interested...

    Posted by Legacy on 05/21/2000 12:00am

    Originally posted by: Tom Archer

    I have lots of little controls like this and will post more if I think people would get any use from them. For example, I have a CUnderLineEdit that basically looks like the underline you might see on a form for a signature and date. I also have a CPageCtrl that looks like a 3D sheet of paper drawn on a dialog (used to present form-like UIs such as Invoice and Purchase Orders).

    Anyway, like I said, I have lots more little UI gadgets that I can post if people are interested in me posting them.

    Tom Archer
    Web Master - CodeGuru

  • Nice!

    Posted by Legacy on 05/18/2000 12:00am

    Originally posted by: Abu W.

    Nice way to save space!

  • Great work!!

    Posted by Legacy on 05/17/2000 12:00am

    Originally posted by: Richard

    This is one of those controls you don't realize you need until you see it!! Thanks and great work!!


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Like many roles in the C-Suite, the CIO role is also changing. Its impact and control have been circumvented by cloud computing, the consumerization of IT, and employees that now have unprecedented access to technology. The role of the CIO is at a critical juncture. For some CIOs, the changing landscape reduces their scope of influence. For others, it means unprecedented opportunity. This eBook explores the changes that CIOs are experiencing today and how these executives can leverage their expertise to help …

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud–based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date