Click here to Skip to main content
13,407,999 members (52,219 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 4 Oct 2000

Number, Currency, Percentage Edit Control

, 4 Oct 2000
Rate this:
Please Sign up or sign in to vote.
Edit Controls that provide masking, formatting and validating for number, currency, percentage values.
  • Download demo project - 27 Kb
  • Download source - 8 Kb
  • Download demo application - 11 Kb
  • NCPTest Application - Sample Image

    <!-- STEP 3. Add the article text.-->


    Simplify working with numbers, currency and percentage values in edit controls with the CNumEdit, CCurrencyEdit and CPercentageEdit classes referred to collectively in this article as NCPEdit.

    Why is this helpful?

    In a nutshell NCPEdit simplifies working with numbers, currency and percentage data by:

    • Providing robust validation of user input. The precision and sign of the number are able to be stipulated and enforced;
    • Alerting the user to invalid input as it is typed rather then when UpdateData() is called.
    • Formatting the currency and percentage values appropriately once the edit control looses focus and removing the formatting when the edit control receives focus;
    • Allowing you as the programmer to think in terms of numbers only. The burden of converting between strings and numbers is left to the classes to deal with.

    How do you use it?

    The screen shot shows how you might use NCPEdit in a simple loan repayment application. Note this example also includes basic serialization demonstrating how to get the data in and out of the NCPEdit controls.

    To use the classes follow these steps:

    Step 1

    Add the CNumEdit, CCurrencyEdit and CPercentage source files to your project.

    Step 2

    Add a standard edit control to you dialog/form view.

    Step 3

    Use ClassWizard to add a Control member variable of type CNumEdit or CCurrencyEdit or CPercentageEdit for the edit control you previously added in Step 2. If ClassWizard will not let you do this select CEdit. If you needed to do the latter in your view class change the ClassWizard code added to either CNumEdit, CCurrencyEdit and Cpercentage as required. For example

    	enum { IDD = IDD_NCPTEST_FORM };
    	CCurrencyEdit	edit_loan_amount;
    Step 4

    Use ClassWizard to overide OnInitialUpdate for you view class. Initialise each of the NCPEdit controls. The prototypes for each of intialisation functions are self explanatory and are as follows:

    void CNumEdit::Init(double fNumber, int dPrecision, bool bNegValues)
    void CCurrencyEdit::Init(double	fNumber, int dPrecision,
                            bool bNeg_Values, TCHAR chCurrency_Symbol)
    void CPercentageEdit::Init(double fNumber,int dPrecision, bool bNeg_Values)

    For example

    edit_loan_amount.Init(m_fLoan_Amount, 0, false, '$'); 

    Step 5

    To get and set the value in the NCPedit control call GetNumber() and SetNumber() functions.
    m_fLoan_Amount =  edit_loan_amount.Get_Number();	

    That’s it!!!!

    How does NCPedit work?

    CCurrencyEdit and CPercentageEdit are both derived from CNumEdit. CNumEdit as you probably guessed subclasses good old CEdit. Refer to Chris Maunder’s article Create your own controls - the art of subclassing for the ins and outs of subclassing.

    The flow of the control for all NCPEdit classes is relatively simple and can be summarised as follows:

    When the NCPEdit control recieves focus OnSetfocus() is called and the edit control is updated with the unformated text.

    void CNumEdit::OnSetfocus() 

    As characters are inputed they are checked to ensure they valid.

    void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
    // 1 - Check validity of character typed
    // 2 - Standard OnChar processeing 

    Here the function Validate_Char(nChar) does most of the work.

    When the control looses focus OnKillfocus() is called which does two main things. Firstly it calls Get_EditCtrl_Text() which

    stores the data in its unformatted text form m_strText for use next time OnSetfocus() is called

    and stores it also as a number in the m_fNumber member variable.

    Secondly the function Update() is called which produces the formatted currency or percentage text storing as a (m_strCurrency or m_strPercentage) member variable. The edit control is then updated with this formatted text.

    All of this isn’t too brain straining. Something which may be of interest however to programmers begining in C++ is the functions used to convert between numbers and strings and back again. These can be extremely useful and are presented below. For more information on the format string used in the format function of CString look up "format specification fields, printf" in MSDN.

    double  CNumEdit::Text_To_Number(CString strText)
    	return atof(strText);
    CString CNumEdit::Number_To_Text(double fNumber)
    // 1 - Prepare the format string 
    //	   eg format string %.5f formats a double with the precision set 
    //	   to five (5)
    	CString strFormat;
    // 2 - Format the number to text
    	CString strText;
    	return strText;

    What next?

    Well NCPEdit may be quite simple but as always there is room for improvement. If you have any suggestions or find any bugs please let me know and I will try and correct them. Have fun!


    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

    Ian J Hart
    United States United States
    No Biography provided

    You may also be interested in...


    Comments and Discussions

    QuestionLicense? Pin
    Glaucus18-Aug-09 6:29
    memberGlaucus18-Aug-09 6:29 
    GeneralGood sample Pin
    pank00719-Feb-05 10:28
    memberpank00719-Feb-05 10:28 
    GeneralA little tip Pin
    Anonymous25-Jun-01 23:47
    memberAnonymous25-Jun-01 23:47 
    GeneralDialog update "problem" Pin
    Matthew Berry1-Feb-01 4:43
    memberMatthew Berry1-Feb-01 4:43 
    GeneralRe: Dialog update "problem" Pin
    brusa19-Dec-04 11:49
    memberbrusa19-Dec-04 11:49 
    GeneralFor Unicode compatible and Get the current value on anytime.. Pin
    C.Y. Liu6-Dec-00 17:08
    memberC.Y. Liu6-Dec-00 17:08 
    GeneralFloat to text function Pin
    Simon Hughes11-Oct-00 10:35
    sussSimon Hughes11-Oct-00 10:35 
    Generalbug fix... Pin
    lauren9-Oct-00 10:17
    susslauren9-Oct-00 10:17 

    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 | Terms of Use | Mobile
    Web03 | 2.8.180221.1 | Last Updated 5 Oct 2000
    Article Copyright 2000 by Ian J Hart
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid