How can i restrict a Edit box to enter only floating point numbers?
characters should not be allowed?
Printable View
How can i restrict a Edit box to enter only floating point numbers?
characters should not be allowed?
The Quick way, Derive a class from CEdit, map the WM_KEYDOWN message. Pass all control character like TAB, RETURN BACKSPACE etc.. to the base class handler in CEdit, ignore all alpha characters and only accept numeric and '.' keystrokes. Make sure to check you have only one point.
Alternatively for a proper solution check code guru article below
http://www.codeguru.com/Cpp/controls...icle.php/c503/
Maybe You try to create by ClassWizard a float variable for your editbox this must prevent entering non-numerical characters if I am right.
Just one more link.
Done this way on OnChangeEdit
CString szTemp(m_szThresholdData);
GetDlgItem(IDC_EDITTHRESHHOLD)->GetWindowText(m_szThresholdData);
float f;
char lp[10];
if (sscanf(m_szThresholdData, "%f%s", &f, lp) != 1)
{
if(!m_szThresholdData.IsEmpty())
{
::SendMessage(GetDlgItem(IDC_EDITTHRESHHOLD)->m_hWnd,EM_UNDO,0,0);
GetDlgItem(IDC_EDITTHRESHHOLD)->SetWindowText(szTemp);
}
}
That's not good. What if sscanf returns 0? You want to enter the if's block only if sscanf succesfully parses a float and a string, and returns 2. So it should be:Quote:
if (sscanf(m_szThresholdData, "%f%s", &f, lp) != 1)
{
..
}
or > 1.Code:if (sscanf(m_szThresholdData, "%f%s", &f, lp) == 2)
{
..
}