Click here to Skip to main content
15,884,177 members
Articles / Desktop Programming / MFC
Article

Number, Currency, Percentage Edit Control

Rate me:
Please Sign up or sign in to vote.
4.38/5 (9 votes)
4 Oct 2000 106.1K   4.5K   31   8
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

    Introduction

    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

    public:
    	//{{AFX_DATA(CNcpTestView)
    	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();	
    edit_repayment_amount.Set_Number(m_fRepayment_Amount);

    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() 
    {
    	Set_EditCtrl_Text(m_strText);
    }

    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
    	if(Validate_Char(nChar)!=true)
    	{
    		MessageBeep(0);
    		return;
    	}
    	
    // 2 - Standard OnChar processeing 
    	CEdit::OnChar(nChar,nRepCnt,nFlags);
    } 

    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;
    	strFormat.Format("%d",m_dPrecision);
    	strFormat="%."+strFormat+"f";
    
    // 2 - Format the number to text
    	CString strText;
    	strText.Format(strFormat,fNumber);
    
    	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!

    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
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionLicense? Pin
    Glaucus18-Aug-09 5:29
    Glaucus18-Aug-09 5:29 
    GeneralGood sample Pin
    New Student19-Feb-05 9:28
    New Student19-Feb-05 9:28 
    GeneralA little tip Pin
    25-Jun-01 22:47
    suss25-Jun-01 22:47 
    GeneralDialog update "problem" Pin
    Matthew Berry1-Feb-01 3:43
    Matthew Berry1-Feb-01 3:43 
    GeneralRe: Dialog update "problem" Pin
    brusa19-Dec-04 10:49
    brusa19-Dec-04 10:49 
    GeneralFor Unicode compatible and Get the current value on anytime.. Pin
    6-Dec-00 16:08
    suss6-Dec-00 16:08 
    GeneralFloat to text function Pin
    Simon Hughes11-Oct-00 9:35
    Simon Hughes11-Oct-00 9:35 
    Generalbug fix... Pin
    lauren9-Oct-00 9:17
    lauren9-Oct-00 9: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.