Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / MFC
Article

CEdit Enhancement

Rate me:
Please Sign up or sign in to vote.
4.81/5 (10 votes)
31 Jan 2001 230.9K   2.9K   41   39
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 :-)

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


Written By
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++ and today C#. Works since 1986 as Consultant, between 1990 and 2008 for Infobrain in Switzerland, from 2008 until 2013 for enValue (also Switzerland) and currently working for Comfone (Bern, Switzerland).

Married, two grown-up daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

Comments and Discussions

 
Generalgood Pin
Anonymous6-Jul-05 20:46
Anonymous6-Jul-05 20:46 
GeneralBUG:Can't use point as allowed chars Pin
taralla3-May-04 14:07
taralla3-May-04 14:07 
GeneralRe: BUG:Can't use point as allowed chars Pin
thanhsonkhach1-Jan-07 15:35
thanhsonkhach1-Jan-07 15:35 
GeneralBUG:Can't use point as allowed chars Pin
tarelli3-May-04 14:06
tarelli3-May-04 14:06 
GeneralGood control, but slow... Pin
mrollin30-Dec-02 13:27
mrollin30-Dec-02 13:27 
GeneralBug while filtering input ... Pin
cr9726-Nov-02 4:11
cr9726-Nov-02 4:11 
GeneralRe: My Workaround ... Pin
cr9726-Nov-02 22:42
cr9726-Nov-02 22:42 
GeneralKill Focus Reflection Pin
Christine Hammond27-Aug-02 11:19
sussChristine Hammond27-Aug-02 11:19 
GeneralEsc/Close bug Pin
John W Wilkinson5-Aug-02 0:45
John W Wilkinson5-Aug-02 0:45 
GeneralBug Pin
6-Mar-02 3:29
suss6-Mar-02 3:29 
GeneralRe: Bug - CAutoEdit, CEdit enhancement Pin
john john mackey23-Oct-02 11:53
john john mackey23-Oct-02 11:53 
Generalloop Pin
24-Mar-01 21:28
suss24-Mar-01 21:28 
QuestionBug? Pin
8-Feb-01 21:38
suss8-Feb-01 21:38 
AnswerRe: Bug? Pin
Daniel Zuppinger8-Feb-01 23:05
Daniel Zuppinger8-Feb-01 23:05 
GeneralRe: Bug? Pin
Joaquín M López Muñoz9-Feb-01 0:40
Joaquín M López Muñoz9-Feb-01 0:40 
GeneralRe: Bug? Pin
Daniel Zuppinger9-Feb-01 0:51
Daniel Zuppinger9-Feb-01 0:51 
GeneralNot so sure... Pin
6-Feb-01 7:17
suss6-Feb-01 7:17 
GeneralRe: Not so sure... Pin
6-Feb-01 22:00
suss6-Feb-01 22:00 
GeneralRe: Not so sure... Pin
Daniel Zuppinger6-Feb-01 22:03
Daniel Zuppinger6-Feb-01 22:03 
GeneralRe: Not so sure... Pin
7-Feb-01 6:05
suss7-Feb-01 6:05 
GeneralRe: Not so sure... Pin
Chris Meech7-Feb-01 6:53
Chris Meech7-Feb-01 6:53 
GeneralRe: Not so sure... Pin
7-Feb-01 10:58
suss7-Feb-01 10:58 
GeneralAn Alternative Pin
Ben Hanson29-Jul-04 23:19
Ben Hanson29-Jul-04 23:19 
GeneralPatent it! :) Pin
Chris Losinger2-Feb-01 4:25
professionalChris Losinger2-Feb-01 4:25 
GeneralRe: Patent it! :) Pin
2-Feb-01 22:33
suss2-Feb-01 22:33 

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.