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.

I hope someone else can get some use out of this code, too. :-)

Display Mode

Edit Mode

How to include in your project

  1. Copy NumEdit.cpp and NumEdit.h into your project directory.
  2. Add NumEdit.cpp and NumEdit.h to your project.

How to use

  1. Create an edit control on your dialog.
  2. Declare a variable in your dialog class, like this:
    cNumericEditCtrl m_NumericEdit;
  3. In OnInitDialog, call SubclassDlgItem, like this:
    m_NumericEdit.SubclassDlgItem(IDC_NUMERIC_EDIT, this); // Use your edit control ID here
  4. Set the prefix, suffix, # decimals, and whether commas should be displayed using the following member functions:
    void SetAttributes(short NumDecimalPlaces, BOOL DisplayCommas = TRUE);
    void SetPrefix(CString Prefix);
    void SetSuffix(CString Suffix);
  5. Set (optional) and retrieve the numeric value within your dialog as needed using the following member functions:
    double GetNumericValue();
    void SetNumericValue(double NumericValue);

Download compiled demo app (no project files) - 89 KB

Download demo (project files) - 14 KB

Download source - 4 KB


  • a fatal bug

    Posted by Legacy on 08/30/2003 12:00am

    Originally posted by: hbAi

    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;loop Reply

  • ExEditors Library

    Posted by Legacy on 03/19/2002 12:00am

    Originally posted by: Mike Philips

    A complete collection of editors in a single file:


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

    Posted by Legacy on 06/25/2001 12:00am

    Originally posted by: Avtandil Adamia

    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

    Avtandil Adamia.

  • OnKillFocus - small bug

    Posted by Legacy on 04/11/2001 12:00am

    Originally posted by: Whitney Arnold

    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.

    CString workstr, str, temp;
    double value;
    int nLength;

    // 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;

    // Only remove the stuffix if there,
    // it may have been removed during editing
    nLength = m_Suffix.GetLength();
    temp = str.Right(nLength);
    if (temp == m_Suffix)
    workstr = str.Left (str.GetLength() - nLength);

    if(workstr.GetLength()>0) {
    m_HasValue = TRUE;
    } else {
    m_HasValue = FALSE;

    // properly round and convert
    value = atof(workstr);
    value = RoundToDecimal(value, m_NumDecimalPlaces);

    // Set member value
    m_NumericValue = value;

    // Set text with formatting if selected

  • Notifications: problem solved

    Posted by Legacy on 03/25/1999 12:00am

    Originally posted by: Roel Schroeven

    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.

  • EN_CHANGE Notification

    Posted by Legacy on 01/27/1999 12:00am

    Originally posted by: Steven M. McNeese

    I am unable to get notification for messages on the edit control. I tried mapping a function to the EN_CHANGE using the class wizard for the control on the dialog. What am I doing wrong?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

  • On-demand Event Event Date: March 23, 2017 As you adopt the use of cloud services, whether in public/IaaS, SaaS or hybrid environments, the attack surface expands and, if breached, the costs increase exponentially. This session is designed to help IT and security leaders understand and address the unique challenges that enterprises typically face when they deploy their applications in the public cloud. It summarizes the areas that the public cloud vendors typically take care of and highlights the areas …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date