|| | |||Browse by category|
How do I set up a normal edit cell to accept only numbers?
You have to create your own cell type by subclassing CGXEditControl and overriding the ValidateString() member function. Every time the user changes the contents of the edit control (pasting characters from the clipboard, selecting a group of characters and pressing the Delete key, pressing Backspace, and so on), the ValidateString() function will be called.
If you return FALSE from ValidateString(), the pending change is negated. So, for single characters that are typed, returning FALSE will effectively ignore the character completely (it will never be seen in the grid and the cell value will not change).
Here is an example of a ValidateString() function for a custom edit control that accepts only the digits 0~9 and decimal points:
|BOOL CNumericEditControl::ValidateString(const CString& sEdit)
|// Cycle through string and check each character if it is a digit
for (int n = 0; n < sEdit.GetLength(); n++)
|if (!isdigit(sEdit[n]) && sEdit[n] != '.')|
NOTE: The masked edit control (CGXMaskControl) can also be used to restrict user input. A template or mask is defined for the cell that determines which kinds of characters can be entered at what position(s) in the string. If the user attempts to enter an invalid character (such that the new cell content no longer fits the mask), then the character is automatically ignored. The CGXMaskControl method involves less effort (you will not have to subclass). However, overriding the ValidateString() method is more flexible, since you will have more control over how the validation is done. Check the class reference for CGXMaskControl for more information about the masked edit control's capabilities.