Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC
Article

A Ready Made Text Entry Dialog

Rate me:
Please Sign up or sign in to vote.
4.40/5 (9 votes)
26 Jan 20023 min read 91K   2.1K   25   7
An article providing a ready made class for simple user input of a string via a prompt dialog - no dialog templates necessary!

Text entry dialog

Demo application

Introduction

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.

Usage

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:

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.

Conclusion

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

Updates

  • 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

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


Written By
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.

Comments and Discussions

 
QuestionThank you Pin
cjv1233-Sep-16 10:11
cjv1233-Sep-16 10:11 
GeneralThank you Pin
Ramprasad_N2-Nov-14 23:44
Ramprasad_N2-Nov-14 23:44 
GeneralFocus/Capture Fix Pin
llllskywalker8-Oct-03 10:02
llllskywalker8-Oct-03 10:02 
GeneralLoss of focus Pin
David Fleming14-Nov-01 9:16
David Fleming14-Nov-01 9:16 
First, let me say that this "input box" is just what
I've been looking for. Very straightforward, simple
and easy to use. Thanks.

However, the parent program does not regain
the focus when the box is closed (either with
OK or Cancel). I noticed that the other message
mentions the same thing. I too am running under
Windows ME.

Any thoughts?

Regardless, this is cool. Thanks.
GeneralRe: Loss of focus Pin
Andrew Peace14-Nov-01 10:08
Andrew Peace14-Nov-01 10:08 
GeneralRe: Loss of focus Pin
Andrew Peace28-Jan-02 7:17
Andrew Peace28-Jan-02 7:17 
GeneralFocus problem Pin
22-Jul-01 23:20
suss22-Jul-01 23: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.