Pastable Numeric Edit Control

I've noticed several postings lately concerning the lack of customized edit controls that handle pasting.
This numeric edit control handles pasting. There are settings within the control for the number of decimal
places to allow, whether to include commas, a prefix string, and a suffix string. When in
display mode (i.e. not active), the control displays the number formatted with a prefix, suffix, comma, and #
decimal places. When in edit mode, the control displays only the number value for the control- checking that
all inputs are valid numeric characters.

I wrote this control to replace the boring edit controls we were using for entering
numbers. I have included source files, a demo project, and a demo executable for this control.

Comments

a fatal bug

if a number such as 0.032 is typed in a text box ,when the box lose focus, the number 0.032 will be multiply 10^m_NumDecimalPlaces, i.e when m_NumDecimalPlaces is 3 the number will be 32 in the text box which lose focus.

a coorection

Posted by rachid1245
on 10/09/2007 08:36am

Hi everybody,
Before excuse me for my bad English
the problem in "a fatal bug" refers to the function SetNumericText(BOOL ShowFormatting)
in fcvt(m_NumericValue,m_NumDecimalPlaces,&decimal,&sign)decimal can be <0, so in
for(loop=0;loopReply

There is deen for checking "-" (minus), if it is already used.

There are small bug.
In the Function DoesCharacterPass you are cheking "-" (minus) only by position.
If I will enter minus at the position 0, after this set cursor at the position 0 and agayn enter "-" (minus),
I will have result --1234.0000

OnKillFocus - small bug

Thanks for the code. I was able to use this in my project but I did have to fix one small bug which probably doesn't occur much. This has to do with when the control looses and gains focus.

In the OnKillFocus, you get the value in the edit box and use atof to convert that to a double to store in m_NumericValue. However, if the edit text is still using the suffix or a prefix, atof cannot handle that and returns 0, so the control looses its value. Here's my version of OnKillFocus to get around that. This is probably not the cleanest code, but it does work.

GetWindowText(str);
// Just in case there are extra spaces
str.TrimLeft(); str.TrimRight();

// First remove any prefix or suffix
// before seeing there is a value
workstr = str;
if (!m_Prefix.IsEmpty()) {
nLength = m_Prefix.GetLength();
temp = str.Left(nLength);
// Only remove the prefix if it is there,
// it may have be removed during editing
if (temp == m_Prefix)
workstr = str.Mid (nLength);
}
if (!m_Suffix.IsEmpty()) {
// In case there was a prefix as well
str = workstr;

Notifications: problem solved

The dialog that owns the control didn't receive notifications for EN_KILLFOCUS, EN_SETFOCUS and EN_CHANGE.
That problem is now solved. Here are the changes you need to apply to make it work:

Change ON_CONTROL_REFLECT to ON_CONTROL_REFLECT_EX for the three notifications in the message map in
NumEditC.cpp. Change the type of the three message handler functions in NumEditC.h from afx_msg void to
afx_msg BOOL, and from void to BOOL in NumEditC.cpp. Now edit the function bodies so that they return FALSE.
That's it.

Top White Papers and Webcasts

Intelligent N+X Redundancy, Placement Affinities, & Future Proofing in the Virtualized Data Center
Virtualization brought about the ability to simplify business continuity management in IT. Workload portability and data replication capabilities mean that physical infrastructure failures no longer need impact application services, and they can rapidly be recovered even in the event of complete site failure.
However, Enterprises and Service Providers face new challenges ensuring they have enough compute …

The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals:
Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals.
IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices.
Job satisfaction is the No. 1 predictor of performance against organizational …