Numeric edit and numeric spin controls

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

In my application I needed an edit control which accepts only numeric numbers. In many science applications this is indispensable, but it is actually not implemented in MS-Visual C/C++. It is not difficult to implement this control and I see that some people implemented it. In my own code, some new features were enriched as error messages and range defines, etc.
Note: to use the CNumEdit class properly, the ES_NUMBER, ES_MULTILINE must not be set.

Another control added in my work is CNumSpinCtrl which enables using a spin control as a numeric one which is also not implemented in MFC.

Note: to use the CNumSpinCtrl class properly, the ES_AUTOBUDDY, ES_SETBUDDYINTEGER must not be set.

Your suggestions are appreciated. Enjoy!

class CNumEdit : public CEdit
{
DECLARE_DYNAMIC(CNumEdit)
public:
CNumEdit();
virtual ~CNumEdit();
enum {VALID = 0x00, OUT_OF_RANGE = 0x01, INVALID_CHAR = 0x02};
virtual void ChangeAmount(int step);
virtual float GetDelta();
virtual void SetDelta(float delta);
virtual void GetRange(float &min, float &max)const;
virtual void SetRange(float min, float max);
virtual void Verbose(BOOL v);
virtual BOOL Verbose()const;
virtual int IsValid()const;
virtual int IsValid(const CString &str)const;
virtual BOOL SetValue(float val);
virtual float GetValue()const;
protected:
virtual CString& ConstructFormat(CString &str, float num);
BYTE m_NumberOfNumberAfterPoint;
BOOL m_Verbose;
float m_Delta, m_MinValue, m_MaxValue;
DECLARE_MESSAGE_MAP()
};

class CNumSpinCtrl : public CSpinButtonCtrl
{
DECLARE_DYNAMIC(CNumSpinCtrl)
public:
CNumSpinCtrl();
virtual ~CNumSpinCtrl();
virtual void SetDelta(float delta);
virtual float GetDelta();
virtual void SetBuddy(CNumEdit *edit);
virtual CNumEdit* GetBuddy() const;
virtual void GetRange(float &lower, float& upper ) const;
virtual void SetRange(float nLower, float nUpper );
virtual float GetPos();
virtual void SetPos(float val);
};

Download demo project – 16 KB

Date Last Updated: May 17, 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read