Click here to Skip to main content
Click here to Skip to main content

CEdit Enhancement

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

Introduction

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() 
{
    CDialog::OnInitDialog();

    // Set up the behaviour for each edit field
    
    // leave the field after 2 (valid) characters have been entered
    m_Edit1.SetMaxChars(2);
    // allow all numeric characters
    m_Edit1.SetValidChar(_T("0123456789"));
    // create a validation interface
    m_dayValidate = new CDayValidate;
    // set the validation handler
    m_Edit1.SetValidationHandler(m_dayValidate);
    // start with this value
    m_Edit1.SetDefaultValue(_T("01"));
    // move the default into the field
    m_Edit1.SetText();

    m_Edit2.SetMaxChars(2);
    m_Edit2.SetValidChar(_T("0123456789"));
    m_monthValidate = new CMonthValidate;
    m_Edit2.SetValidationHandler(m_monthValidate);
    m_Edit2.SetDefaultValue(_T("01"));
    m_Edit2.SetText();

    m_Edit3.SetMaxChars(4);
    m_Edit3.SetValidChar(_T("0123456789"));
    m_yearValidate = new CYearValidate;
    m_Edit3.SetValidationHandler(m_yearValidate);
    m_Edit3.SetDefaultValue(_T("2000"));
    m_Edit3.SetText();

    m_Edit4.SetMaxChars(8);
    m_Edit4.SetValidChar(_T("10"));
    m_binValidate = new CBinValidate;
    m_Edit4.SetValidationHandler(m_binValidate);

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

Problems

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.

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

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

Comments and Discussions

 
GeneralRe: Bug - CAutoEdit, CEdit enhancement Pinmemberjohn john mackey23-Oct-02 11:53 
Generalloop PinmemberRobert M. Bouwens24-Mar-01 21:28 
QuestionBug? PinmemberJoaquín M. López Muñoz8-Feb-01 21:38 
AnswerRe: Bug? PinmemberDaniel Zuppinger8-Feb-01 23:05 
GeneralRe: Bug? PinmemberJoaquín M López Muñoz9-Feb-01 0:40 
GeneralRe: Bug? PinmemberDaniel Zuppinger9-Feb-01 0:51 
GeneralNot so sure... PinmemberJohn Wood6-Feb-01 7:17 
GeneralRe: Not so sure... PinmemberAnonymous6-Feb-01 22:00 
GeneralRe: Not so sure... PinmemberDaniel Zuppinger6-Feb-01 22:03 
GeneralRe: Not so sure... PinmemberJohn Wood7-Feb-01 6:05 
GeneralRe: Not so sure... PinmemberChris Meech7-Feb-01 6:53 
GeneralRe: Not so sure... PinmemberJohn Wood7-Feb-01 10:58 
GeneralAn Alternative PinmemberBen Hanson29-Jul-04 23:19 
GeneralPatent it! :) PinmemberChris Losinger2-Feb-01 4:25 
GeneralRe: Patent it! :) PinmemberAnonymous2-Feb-01 22:33 
GeneralMy God , you're a genius PinmemberAmit Nabarro1-Feb-01 11:58 
GeneralRe: My God , you're a genius PinmemberAnonymous5-Feb-01 11:04 
GeneralMore worthless crap from annonymous sources PinmemberAndy Metcalfe6-Feb-01 5:51 
GeneralRe: More worthless crap from annonymous sources PinmemberNorm Almond6-Feb-01 6:44 
GeneralRe: More worthless crap from annonymous sources PinmemberDaniel Zuppinger6-Feb-01 22:07 
GeneralRe: My God , you're a genius PinmemberAnonymous8-Feb-01 11:28 
GeneralSuggestion PinmemberPavel Voronkov20-Dec-00 0:16 
GeneralRe: Suggestion PinmemberDaniel Zuppinger20-Dec-00 19:55 
GeneralRe: Suggestion PinmemberAttila Hajdrik3-Feb-01 10:45 
GeneralRe: Suggestion PinmemberFrank Feng15-Nov-01 16:45 
GeneralLittle correction in InfoWnd.cpp PinmemberDaniel Zuppinger14-Dec-00 21:38 
GeneralRe: Little correction in InfoWnd.cpp PinmemberSteve Johnson18-Dec-00 9:14 
GeneralRe: Little correction in InfoWnd.cpp PinmemberDaniel Zuppinger18-Dec-00 19:28 
GeneralRe: Little correction in InfoWnd.cpp PinmemberAnonymous17-Sep-01 5:00 

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
Web01 | 2.8.140827.1 | Last Updated 1 Feb 2001
Article Copyright 2000 by Daniel Zuppinger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid