Click here to Skip to main content
13,287,343 members (103,342 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 17 Jul 2001

A Ready Made Text Entry Dialog

, 26 Jan 2002
Rate this:
Please Sign up or sign in to vote.
An article providing a ready made class for simple user input of a string via a prompt dialog - no dialog templates necessary!
<!-- Download Links -->

Text entry dialog

Demo application


There are several occasions when I have been programming an application, and have needed to have the user input a single line of text, for instance the name of a new object to create. To start with, I created a dialog template for each instance, with the correct title and controls. Then I realised that this was very wasteful in terms of code, resources and time. So I created a universal dialog template and class that would accept user input given a prompt and window title. However, since then, I have realised that even this is not ideal – every time you need to use that class, you have to copy the dialog resource to the new project, hence the CTextEntryDlg class, inspired by Chris Maunder’s progress window class, was born.


The class is extremely simple to use. First, in your .cpp file, add an #include directive as follows:

#include <TextEntryDlg.h>

Now, you need to create an instance of the class. This is simply done by declaring a variable of the type CTextEntryDlg, as follows:

CTextEntryDlg dlgTextEntry;

The next stage is to display the dialog. This is done by calling the Show(...) function. This has the following signature:

int Show(CWnd *pParent, LPCTSTR pszTitle, LPCTSTR pszPrompt, 
         LPCTSTR pszDefault = _T(""), bool bPassword = false)

In the pParent parameter, we pass a pointer to the parent window. This is to ensure the dialog’s modal loop works correct. The pszTitle parameter should be set to a string that you would like for the title-bar of the window. The pszPrompt parameter should be set to a string that you would like to use for the prompt, to be place in a static control positioned just above the edit control. The pszDefault parameter is a string that should be used to begin with. If none is supplied, the edit box is empty when the dialog is created. If the bPassword parameter is true, the edit box will be created with the ES_PASSWORD style.

When this function returns, you can determine whether the dialog was cancelled, and what string the user entered. Here are the possible return values:

<table width=100% border=1> 0 The dialog was not successfully created. IDOK The dialog was dismissed with the OK button, or the user pressed ENTER IDCANCEL The dialog was dismissed with the Cancel button, or the user pressed ESCAPE

Once you have determined that the user clicked OK, you can obtain the string which was entered using the GetText() function (the function will also return the correct string even if Cancel was selected). The GetText() function returns a LPCTSTR for simplicity, but you can put this straight into a CString if you like:

CString strResult = dlgTextExtry.GetText();

That’s basically all there is to it!

Under the Hood

The inner workings of the class are relatively simple. Basically, in the Show() function, the controls that make up the window are dynamically created. Important points to note here are the use of CreateEx for the edit control, because otherwise it could not be created with a ‘client’ edge, and the way in which the font is specifically set for each control.

The function then calls the private function DoModal(), which basically disables the parent window, and then calls RunModalLoop(). The interaction with Windows from then on is purely message based. However, a further important point to notice is the additional code in PreTranslateMessage. This exists to process escape and tab key-presses, and is lifted almost with change from dlgcore.cpp in the MFC source files.


I hope you find this article and its accompanying source code useful, and if you have any questions, feel free to mail me (click on my name at the top of the page).


  • January 27th, 2002
    • Fixed problem with focus not returning to parent in WinME
    • Fixed flicker problem in Windows 2000 and XP
    • Corrected routine that calculates height of the dialog


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


About the Author

Andrew Peace
United Kingdom United Kingdom
Andrew is currently a student at the University of Cambridge, working towards a degree in Computer Science. The word 'working' is used here in a vague sense, with the hope that the reader will realise that the same sentence contained the word 'student'.

Aside from computing he has a strong interest in music, and enjoys the outdoors, particularly when the weather permits going out in them... To Andrew, cycling is fun, sailing is more fun, and the odd camping trip and walk is certainly what the doctor ordered.

In terms of programming experience, he first started writing programs for the Commodore Amiga using BASIC, after which he learned the joys of C and has never looked back. Since, he has added C++ and C# to his repotoire, along with a bunch of other crazy languages like ML that they like to teach in college.

You may also be interested in...


Comments and Discussions

GeneralFocus problem Pin
Anonymous23-Jul-01 0:20
memberAnonymous23-Jul-01 0:20 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 27 Jan 2002
Article Copyright 2001 by Andrew Peace
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid