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