Click here to Skip to main content
15,880,796 members
Articles / Desktop Programming / MFC
Article

CGridCellNumeric - A numeric cell class for the MFC Grid

Rate me:
Please Sign up or sign in to vote.
4.81/5 (10 votes)
22 Sep 2004CPOL5 min read 106.6K   3.7K   26   31
A locale aware, editable, self validating numeric cell class for the MFC Grid. Configurable for integers, floating point, or currencies.

Introduction

I currently have had a need for a decent grid control that has the ability to edit and display numbers, among other things. The best one that I could come up with within my price range (free) was Chris Maunder's MFC Grid 2.25 [^]. But unfortunately, for me it needed some modifications to get it to work the way I wanted. The one modification I present here is the CGridCellNumeric class, which is used to display and edit numbers and currencies. This class is a severe modification of the CGridCellNumeric class that is included with the MFC Grid. The class uses the computer's locale settings to display the numbers, but does provide a way for you to override those settings. The numbers, if you set the cell to use floating point, will be kept to an accuracy of nine decimal places, even if they are only displayed to two. The full nine decimal digits are shown, if needed, in the edit control part of the cell.

Input validation is done on the fly as the user is typing. All validation is done in the EN_UPDATE message handler, so any cut/copy and paste operations are immediately validated. If the user deletes the text in the cell, the value is set to zero.

Using the CGridCellNumeric class

Download and unzip the file, and save the CPP and header file into the "GridCtrl/NewCellTypes" folder. They can replace the existing GridCellNumeric files already there. I used those existing files as the starting point for this code.

To add a CGridCellNumeric cell to the grid, simply add it as you would any other cell type:

m_Grid.SetCellType(1,1, RUNTIME_CLASS(CGridCellNumeric));

and you are ready to go. To set the options for the numerical cell, you have to get a pointer to it, and call one of its member functions.

CGridCellNumeric *pCell = (CGridCellNumeric *)m_Grid.GetCell(1, 1);
pCell->SetFlags(CGridCellNumeric::Currency | CGridCellNumeric::Negative);
pCell->SetNumber(125.36);

The CGridCellNumeric class is derived from CGridCell, and the edit control class (CInPlaceNumEdit) is derived from CInPlaceEdit, so other than the API functions listed below, you can use this cell the same as any other editable cell that is available for the MFC Grid.

Important changes to class CInPlaceEdit

There is one small, but very important change that has to be made to the CInPlaceEdit class. You must move the CRect m_Rect variable from being a private member to being a protected member of the CInPlaceEdit class. This has to done so that the derived CInPlaceNumEdit class can reset the initial size of the edit box if needed, and it needs access to the m_Rect member in order to do so. So in the InPlaceEdit.h file, make the following changes:

class CInPlaceEdit : public CEdit
{
    ...

protected:
    CRect m_Rect;   // Add this line
    ...

private:
    ...
//  CRect m_Rect; // comment out, or remove, this line
};

Also, but not as important, as it is more a cosmetic thing than anything else, is to change the size of the space buffer that CInPlaceEdit uses to calculate the new size of the edit control as you are typing. Go to the file InPlaceEdit.cpp, in the function CInPlaceEdit::OnChar(), line 178, and change the size of the buffer from two spaces to six spaces:

// change from:
//  str += _T("  ");
//  to
    str += _T("      ");

This change will prevent the edit control from scrolling the text to the left before it resizes the window.

CGridCellNumeric Class API functions

  • DWORD SetFlags (DWORD dwFlags)

    Sets the flags that controls how the cell operates. Possible flags are:

    • CGridCellNumeric::Integer - The cell handles only integers, no floating point numbers can be displayed or entered.
    • CGridCellNumeric::Real - The cell handles floating point numbers.
    • CGridCellNumeric::Currency - The cell handles currency numbers.

      Please note that these first three values are mutually exclusive, and if they are not specified when you place a call to SetFlags, the CGridCellNumeric::Integer flag is assumed by default.

    • CGridCellNumeric::Negative - The cell can handle negative numbers. If this flag is not specified, the cell will handle positive numbers only.
    • CGridCellNumeric::Silent - If this flag is not set, the edit control associated with the CGridCellNumeric cell will emit a warning beep if an invalid entry is made. Use this flag to silence that beep.

    Return Value:

    A DWORD containing the previous flags, -1 if the new flags could not be set.

  • DWORD GetFlags()

    Gets the current flags. See SetFlags above for possible values.

    Return Value:

    A DWORD containing the current flags.

  • BOOL SetNumber (double Number)

    Sets the value of the number displayed in the cell. Even though it is stored as a double, the accuracy is guaranteed to only nine decimal points. Unless specified by SetNumberFmt() or SetCurrencyFmt() functions (see below), the numbers will be displayed according to the computer's locale settings, but the accuracy of the internal number is guaranteed to nine decimal points, and those decimal points are all shown in the edit control.

    Return Value:

    TRUE if the number was successfully set. FALSE if not.

  • double GetNumber()

    Gets the number currently stored in the cell. This number is guaranteed accurate to nine decimal points.

    Return Value:

    A double containing the number in the cell.

  • void SetNumberFmt (LPNUMBERFMT lpNumberFmt)

    Use this function if you want to override the computer's default settings for how integers and floating point numbers are displayed. You can pass in a pointer to a NUMBERFMT structure that is used by the GetNumberFormat() function. Look up GetNumberFormat() and NUMBERFMT in MSDN for more information.

  • void SetCurrencyFmt (LPCURRENCYFMT lpCurrencyFmt)

    Use this function if you want to override the computer's default settings for how currencies are displayed. You can pass in a pointer to a CURRENCYFMT structure that is used by the GetCurrencyFormat() function. Look up GetCurrencyFormat() and CURRENCYFMT in MSDN for more information.

TODO:

Don't know if I will ever get to it, but I think it would be cool if the cell supported different based numbers other than just base 10. Another possibility is to support exponential format. But that is probably going to be more work than what I am willing to put out.

Credits

  • Chris Maunder for his excellent grid control.
  • Andrew Truckle for his original CGridCellNumeric class that this class is based on.

Updates

  • Sept 15, 2004 - released into the wild.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
Canada Canada
Father of two, brother of two, child of two.
Spouse to one, uncle to many, friend to lots.
Farmer, carpenter, mechanic, electrician, but definitely not a plumber.
Likes walks with the wife, board games, card games, travel, and camping in the summer.
High school graduate, college drop-out.
Hobby programmer who knows C++ with MFC and the STL.
Has dabbled with BASIC, Pascal, Fortran, COBOL, C#, SQL, ASM, and HTML.
Realized long ago that programming is fun when there is nobody pressuring you with schedules and timelines.

Comments and Discussions

 
Questionimplied decimal for numeric field Pin
Ian Sutcliffe15-Oct-15 12:46
Ian Sutcliffe15-Oct-15 12:46 
AnswerRe: implied decimal for numeric field Pin
PJ Arends16-Oct-15 7:49
professionalPJ Arends16-Oct-15 7:49 
Questionaccess violation after SetNumberFmt() and SetNumber() Pin
wiwi6221-Jan-13 23:29
wiwi6221-Jan-13 23:29 
AnswerRe: access violation after SetNumberFmt() and SetNumber() Pin
wiwi6221-Jan-13 23:53
wiwi6221-Jan-13 23:53 
QuestionSetNumberFmt Pin
Ian Sutcliffe26-Jun-12 16:59
Ian Sutcliffe26-Jun-12 16:59 
AnswerRe: SetNumberFmt Pin
PJ Arends27-Jun-12 6:03
professionalPJ Arends27-Jun-12 6:03 
GeneralRe: SetNumberFmt Pin
Ian Sutcliffe27-Jun-12 11:36
Ian Sutcliffe27-Jun-12 11:36 
GeneralExcellent! Pin
urb012330-May-07 8:10
urb012330-May-07 8:10 
GeneralEdit cells in virtual mode Pin
anaigir6-Apr-07 2:38
anaigir6-Apr-07 2:38 
GeneralVisual Studio 2005 compile error Pin
WhiTeWo1F24-Nov-05 22:27
WhiTeWo1F24-Nov-05 22:27 
I get this error on vs2005
gridctrl\gridcellnumeric.cpp(798) : error C2440: 'initializing' : cannot convert from 'const char *' to 'TCHAR *'
Conversion loses qualifiers
GridCtrl.cpp

How can I fix this? Is it compiler settings?
GeneralRe: Visual Studio 2005 compile error Pin
PJ Arends25-Nov-05 7:19
professionalPJ Arends25-Nov-05 7:19 
GeneralRe: Visual Studio 2005 compile error Pin
WhiTeWo1F25-Nov-05 9:31
WhiTeWo1F25-Nov-05 9:31 
GeneralRe: Visual Studio 2005 compile error Pin
urb012330-May-07 8:25
urb012330-May-07 8:25 
GeneralCrash when deleting Pin
WhiTeWo1F19-Nov-05 13:40
WhiTeWo1F19-Nov-05 13:40 
GeneralRe: Crash when deleting Pin
PJ Arends19-Nov-05 14:47
professionalPJ Arends19-Nov-05 14:47 
GeneralMath problem Pin
New Student6-Sep-05 3:08
New Student6-Sep-05 3:08 
AnswerRe: Math problem Pin
PJ Arends6-Sep-05 5:37
professionalPJ Arends6-Sep-05 5:37 
GeneralComma as decimal separator doesn't work Pin
A. N.2-Oct-04 11:21
A. N.2-Oct-04 11:21 
GeneralRe: Comma as decimal separator doesn't work Pin
PJ Arends2-Oct-04 16:47
professionalPJ Arends2-Oct-04 16:47 
GeneralRe: Comma as decimal separator doesn't work Pin
A. N.2-Oct-04 21:44
A. N.2-Oct-04 21:44 
GeneralRe: Comma as decimal separator doesn't work Pin
PJ Arends4-Oct-04 10:31
professionalPJ Arends4-Oct-04 10:31 
GeneralRe: Comma as decimal separator doesn't work Pin
rm21-Dec-04 23:12
rm21-Dec-04 23:12 
GeneralRe: Comma as decimal separator doesn't work Pin
PJ Arends2-Dec-04 10:03
professionalPJ Arends2-Dec-04 10:03 
GeneralRe: Comma as decimal separator doesn't work Pin
Andre xxxxxxx22-Mar-05 5:53
Andre xxxxxxx22-Mar-05 5:53 
GeneralRe: Comma as decimal separator doesn't work Pin
PJ Arends22-Mar-05 8:19
professionalPJ Arends22-Mar-05 8:19 

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.