Click here to Skip to main content
11,926,429 members (55,671 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked

CEdit Enhancement

, 31 Jan 2001
Rate this:
Please Sign up or sign in to vote.
A CEdit enhancement for efficient data capturing.


Ever been bothered by message boxes popping up after entering a value in an edit field, telling you that the value is out of range? Or ever got annoyed by a masked edit field while trying to correct a character in the middle of the formatted string? Maybe this approach will give you the solution you where looking for.

sample screen shot

The CAutoEdit class is derived from the normal CEdit class.

With the method SetValidChar, you can define which characters may be entered by the user, e.g. numeric characters only, or numeric characters, decimal point and sign, or the characters valid for representing a hexadecimal string. The method SetMaxChar lets you define the maximal number of characters expected in this field. Is this number reached, the focus is automatically set to the next field, allowing efficient data capturing. The next method SetExitChar lets you define one additional character which will be used to leave this field, just as pressing the TAB would. This is handy where you want to use the decimal point to skip to the next field etc.

Now the real invention is the error handling: CAutoEdit is using a validation interface class CEditValidate with a virtual function validate. This class can be attached to the CAutoEdit with the method SetValidationHandler. Whenever the CAutoEdit loses the focus, the validation method of the attached validation interface class is called. Here the contents of the field are validated, formatted and if necessary an error message set. If an error message is generated, two things will happen:

  1. The focus stays in the erroneous field and the contents will be selected.
  2. The error message is displayed in a little window just above the faulty field. The class CInfoWnd is used for this.

This behavior allows the user to fix the problem without having to get rid of a message box first and trying to remember the message. The message is right there and will only disappear after the field has been corrected. The user will not be able to select any other field before the problem is corrected with the exception of the cancel button.

There are some other methods to define the behavior of the signs, null padding etc., which I will not describe in detail, they are simple to use and easy to understand.

In order to use this class in you own project, you have to include the files autoedit.h, autoedit.cpp, infowmd.h and infownd.cpp. In your dialog, change your CEdit fields to CAutoEdit and add the initialization in the OnInitDialog:

BOOL CTestDlg::OnInitDialog() 

    // Set up the behaviour for each edit field
    // leave the field after 2 (valid) characters have been entered
    // allow all numeric characters
    // create a validation interface
    m_dayValidate = new CDayValidate;
    // set the validation handler
    // start with this value
    // move the default into the field

    m_monthValidate = new CMonthValidate;

    m_yearValidate = new CYearValidate;

    m_binValidate = new CBinValidate;

    return TRUE;  // return TRUE unless you set the focus to a control

Of course there are many possible improvements to this class, feel free to use and enhance it, just don't forget to share it here Smile | :)


There are a few problems worth mentioning, maybe someone finds a (simple?) solution: For one, when a InfoWnd displays a message and the dialog is moved, the InfoWnd does not follow the dialog. The other problem is that pressing the 'Enter' key closes the dialog, even if there is still a invalid value in one of the CAutoEdit fields.

Note: There is one thing to know: You can not have the cancel button immediately following a CAutoEdit field in the TAB order, otherwise the validation will not work. The reason is that the cancel button is passed to the OnKillFocus method as the next control. This is used to determine if the user has cancelled the dialog and I can not determine if the user pressed the button or the focus is just passed there because it is the next control in line.


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

Daniel Zuppinger
Chief Technology Officer
Switzerland Switzerland
Professional IT developer since 1983. First projects with Cobol, then Pascal, Modula2, C and since Visual C++ 1.0 also with C++. Works since 1986 as Consultant, between 1990 and 2008 for Infobrain in Switzerland, currently employed by enValue (also Switzerland).
Current field is the project management and development of software for Bank applications.
Married, two daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

You may also be interested in...

Comments and Discussions

Generalgood Pin
Anonymous6-Jul-05 21:46
sussAnonymous6-Jul-05 21:46 
GeneralBUG:Can't use point as allowed chars Pin
tarelli3-May-04 15:07
susstarelli3-May-04 15:07 
GeneralRe: BUG:Can't use point as allowed chars Pin
thanhsonkhach1-Jan-07 16:35
memberthanhsonkhach1-Jan-07 16:35 
GeneralBUG:Can't use point as allowed chars Pin
tarelli3-May-04 15:06
membertarelli3-May-04 15:06 
GeneralGood control, but slow... Pin
mrollin30-Dec-02 14:27
membermrollin30-Dec-02 14:27 
GeneralBug while filtering input ... Pin
cr9726-Nov-02 5:11
membercr9726-Nov-02 5:11 
GeneralRe: My Workaround ... Pin
cr9726-Nov-02 23:42
membercr9726-Nov-02 23:42 
GeneralKill Focus Reflection Pin
Christine Hammond27-Aug-02 12:19
sussChristine Hammond27-Aug-02 12:19 
GeneralEsc/Close bug Pin
John W Wilkinson5-Aug-02 1:45
memberJohn W Wilkinson5-Aug-02 1:45 
Nice work but the demo program freezes for a few seconds then crashes if the dialog is closed while the date field has a invalid year. By 'closed' I mean the pressing of the Esc key or the close button in the dialog's title bar.

I have a similar problem with my code. (I was hoping your's would give a solution.) The problem in my code is that when closing a dialog OnClose is called before OnKillFocus. This results in the window being closed before you are able to check for a valid range. (When the Ok button is pressed OnKillFocus is called before OnOk.)


GeneralBug Pin
S. Ganapathi Raman6-Mar-02 4:29
memberS. Ganapathi Raman6-Mar-02 4:29 
GeneralRe: Bug - CAutoEdit, CEdit enhancement Pin
john john mackey23-Oct-02 12:53
memberjohn john mackey23-Oct-02 12:53 
Generalloop Pin
Robert M. Bouwens24-Mar-01 22:28
memberRobert M. Bouwens24-Mar-01 22:28 
QuestionBug? Pin
Joaquín M. López Muñoz8-Feb-01 22:38
memberJoaquín M. López Muñoz8-Feb-01 22:38 
AnswerRe: Bug? Pin
Daniel Zuppinger9-Feb-01 0:05
memberDaniel Zuppinger9-Feb-01 0:05 
GeneralRe: Bug? Pin
Joaquín M López Muñoz9-Feb-01 1:40
memberJoaquín M López Muñoz9-Feb-01 1:40 
GeneralRe: Bug? Pin
Daniel Zuppinger9-Feb-01 1:51
memberDaniel Zuppinger9-Feb-01 1:51 
GeneralNot so sure... Pin
John Wood6-Feb-01 8:17
memberJohn Wood6-Feb-01 8:17 
GeneralRe: Not so sure... Pin
Anonymous6-Feb-01 23:00
memberAnonymous6-Feb-01 23:00 
GeneralRe: Not so sure... Pin
Daniel Zuppinger6-Feb-01 23:03
memberDaniel Zuppinger6-Feb-01 23:03 
GeneralRe: Not so sure... Pin
John Wood7-Feb-01 7:05
memberJohn Wood7-Feb-01 7:05 
GeneralRe: Not so sure... Pin
Chris Meech7-Feb-01 7:53
memberChris Meech7-Feb-01 7:53 
GeneralRe: Not so sure... Pin
John Wood7-Feb-01 11:58
memberJohn Wood7-Feb-01 11:58 
GeneralAn Alternative Pin
Ben Hanson30-Jul-04 0:19
memberBen Hanson30-Jul-04 0:19 
GeneralPatent it! :) Pin
Chris Losinger2-Feb-01 5:25
memberChris Losinger2-Feb-01 5:25 
GeneralRe: Patent it! :) Pin
Anonymous2-Feb-01 23:33
memberAnonymous2-Feb-01 23:33 
GeneralMy God , you're a genius Pin
Amit Nabarro1-Feb-01 12:58
memberAmit Nabarro1-Feb-01 12:58 
GeneralRe: My God , you're a genius Pin
Anonymous5-Feb-01 12:04
memberAnonymous5-Feb-01 12:04 
GeneralMore worthless crap from annonymous sources Pin
Andy Metcalfe6-Feb-01 6:51
memberAndy Metcalfe6-Feb-01 6:51 
GeneralRe: More worthless crap from annonymous sources Pin
Norm Almond6-Feb-01 7:44
memberNorm Almond6-Feb-01 7:44 
GeneralRe: More worthless crap from annonymous sources Pin
Daniel Zuppinger6-Feb-01 23:07
memberDaniel Zuppinger6-Feb-01 23:07 
GeneralRe: My God , you're a genius Pin
Anonymous8-Feb-01 12:28
memberAnonymous8-Feb-01 12:28 
GeneralSuggestion Pin
Pavel Voronkov20-Dec-00 1:16
memberPavel Voronkov20-Dec-00 1:16 
GeneralRe: Suggestion Pin
Daniel Zuppinger20-Dec-00 20:55
memberDaniel Zuppinger20-Dec-00 20:55 
GeneralRe: Suggestion Pin
Attila Hajdrik3-Feb-01 11:45
memberAttila Hajdrik3-Feb-01 11:45 
GeneralRe: Suggestion Pin
Frank Feng15-Nov-01 17:45
memberFrank Feng15-Nov-01 17:45 
GeneralLittle correction in InfoWnd.cpp Pin
Daniel Zuppinger14-Dec-00 22:38
memberDaniel Zuppinger14-Dec-00 22:38 
GeneralRe: Little correction in InfoWnd.cpp Pin
Steve Johnson18-Dec-00 10:14
memberSteve Johnson18-Dec-00 10:14 
GeneralRe: Little correction in InfoWnd.cpp Pin
Daniel Zuppinger18-Dec-00 20:28
memberDaniel Zuppinger18-Dec-00 20:28 
GeneralRe: Little correction in InfoWnd.cpp Pin
Anonymous17-Sep-01 6:00
memberAnonymous17-Sep-01 6:00 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 1 Feb 2001
Article Copyright 2000 by Daniel Zuppinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid