Click here to Skip to main content
13,591,628 members
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 14 Dec 2000

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


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, from 2008 until 2013 for enValue (also Switzerland) and currently I'm working for Comfone (Bern, Switzerland).

Married, two daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

You may also be interested in...

Comments and Discussions

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

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 | Cookies | Terms of Use | Mobile
Web02 | 2.8.180618.1 | Last Updated 1 Feb 2001
Article Copyright 2000 by Daniel Zuppinger
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid