How to validate CEdit only number input?
I use Visual C++ 4.2 Professional.
I want to validate so that user only able to input numeric values. I've
looked for the keydown notify message, but I could not find one.
I've tried to trap the input by deriving a new class named CEditExt from
CEdit and make OnChar event but it did not work, I've debugged it but when I
press any non-numeric key, the program sequence did not entering the OnChar
event.
What is wrong?
I've attached my codes below:
Thank you very much.
in .cpp file:
// CEditExt
IMPLEMENT_DYNAMIC(CEditExt, CEdit)
BEGIN_MESSAGE_MAP(CEditExt, CEdit)
ON_WM_CHAR()
END_MESSAGE_MAP()
// CEditExt ??????????????? ????????????
void CEditExt::OnChar( UINT nChar, UINT nRepCnt, UINT nFlags )
{
if( nChar < '0' || nChar > '9' )
{
if ( nChar != VK_BACK )
return;
}
CEditExt::OnChar( nChar, nRepCnt, nFlags );
}
in .h file:
class CEditExt : public CEdit
{
DECLARE_DYNAMIC( CEditExt )
public:
CEditExt();
virtual ~CEditExt();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnChar( UINT nChar, UINT nRepCnt, UINT nFlags );
};