Download demo project - 27 Kb Download source - 8 KbDownload demo application - 11 Kb
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:
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)
{
if(Validate_Char(nChar)!=true)
{
MessageBeep(0);
return;
}
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)
{
CString strFormat;
strFormat.Format("%d",m_dPrecision);
strFormat="%."+strFormat+"f";
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!
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.