hi,
am using XEdit box in a xlistctrl ,which enables the user to enter value for modification.
i want to validate the value entered in the XEdit box to be only number .
i used the pretranslate message function but its not working,at times it fails and alphabets and other characters are being allowed to enter.
following is the code.
#include "stdafx.h"
#include "XEdit.h"
#include "DialogManager.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT WM_XEDIT_KILL_FOCUS = ::RegisterWindowMessage(_T("WM_XEDIT_KILL_FOCUS"));
UINT WM_XEDIT_VK_ESCAPE = ::RegisterWindowMessage(_T("WM_XEDIT_VK_ESCAPE"));
BEGIN_MESSAGE_MAP(CXEdit, CEdit)
ON_WM_CHAR()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_KILLFOCUS()
ON_WM_PAINT()
END_MESSAGE_MAP()
CXEdit::CXEdit(CWnd *pParent, LPCTSTR lpszText) :
m_pParent(pParent),
m_strText(lpszText),
m_bMessageSent(FALSE)
{
}
CXEdit::~CXEdit()
{
}
void CXEdit::SendRegisteredMessage(UINT nMsg, WPARAM wParam , LPARAM lParam )
{
BOOL bMessageSent = m_bMessageSent;
m_bMessageSent = TRUE;
if (m_pParent && ::IsWindow(m_pParent->m_hWnd) && !bMessageSent)
m_pParent->SendMessage(nMsg, wParam, lParam);
}
int CXEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_pParent)
{
m_pParent = GetParent();
}
if (m_pParent && ::IsWindow(m_pParent->m_hWnd))
{
CFont * pFont = m_pParent->GetFont();
if (pFont)
SetFont(pFont);
}
else
{
m_pParent = NULL;
}
SetWindowText(m_strText);
SetSel(0, -1);
SetFocus();
SetCapture();
return 0;
}
void CXEdit::OnPaint()
{
CRect rect;
GetClientRect(&rect);
rect.right += 2;
CDC * pDC = GetDC();
pDC->FillSolidRect(&rect, ::GetSysColor(COLOR_WINDOW));
ReleaseDC(pDC);
CEdit::OnPaint();
rect.left -= 1;
rect.top -= 1;
rect.bottom -=1;
pDC = GetDC();
CBrush * pOldBrush = (CBrush *) pDC->SelectStockObject(NULL_BRUSH);
CPen pen(PS_SOLID, 1, ::GetSysColor(COLOR_INACTIVECAPTION)); CPen *pOldPen = pDC->SelectObject(&pen);
pDC->Rectangle(&rect);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
ReleaseDC(pDC);
}
BOOL CXEdit::PreTranslateMessage(MSG* pMsg)
{
<pre lang="c++">
if(pMsg->message==WM_CHAR)
{
if(( pMsg->wParam >= '0' && pMsg->wParam <= '9' ) )
{
return false;
}
else if((pMsg->wParam =='.'))
{
return false;
}
else if(pMsg->wParam == VK_BACK)
{
return false;
}
else
return true;
}
return CEdit::PreTranslateMessage(pMsg);
}
///////////////////////////////////////////////////////////////////////////////
// OnChar
void CXEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// handle escape and return, in case edit does NOT have focus
if (m_pParent)
{
if (nChar == VK_ESCAPE)
{
SendRegisteredMessage(WM_XEDIT_VK_ESCAPE);
return;
}
else if (nChar == VK_RETURN)
{
SendRegisteredMessage(WM_XEDIT_KILL_FOCUS);
return;
}
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
///////////////////////////////////////////////////////////////////////////////
// OnKillFocus
void CXEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
SendRegisteredMessage(WM_XEDIT_KILL_FOCUS);
}
///////////////////////////////////////////////////////////////////////////////
// OnDestroy
void CXEdit::OnDestroy()
{
//XLISTCTRL_TRACE(_T("in CXEdit::OnDestroy\n"));
ReleaseCapture();
CEdit::OnDestroy();
}
kindly anyone suggest me any solution!!