Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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

Share

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.

Comments and Discussions

 
GeneralFocus/Capture Fix Pinmemberllllskywalker8-Oct-03 10:02 
I'm using win2k (NT 5). I was having a problem such that after calling dlg.Show(...) the text entry dialog would appear, but it wouldn't allow me to click on the text entry dialog or do anything else until i clicked on another window first and then back onto the text entry dialog. The problem was that mouse input was being sent to the parent window instead of the newly created text entry dialog.
 
so to fix it add one simple line...
 
int CTextEntryDlg::DoModal(CWnd *pParent)
{
pParent->EnableWindow(false);
EnableWindow(true);
 
ReleaseCapture(); // ADD THIS LINE HERE!!!
 
...
}
 
And I lived happily ever after...
 
P.S. Peace -- It's a handy class... you saved me a lot of time, since i was about to write the class myself because, like you, i was sick of using a template resource everytime i needed to query user input. Thanx a million!
 
cheers,
 
--luke
 


Luke Clemens ( http://cs.montana.edu/~clemens )
GeneralLoss of focus PinmemberDavid Fleming14-Nov-01 9:16 
GeneralRe: Loss of focus PinmemberAndrew Peace14-Nov-01 10:08 
GeneralRe: Loss of focus PinmemberAndrew Peace28-Jan-02 7:17 
GeneralFocus problem PinmemberAnonymous22-Jul-01 23:20 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 27 Jan 2002
Article Copyright 2001 by Andrew Peace
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid