-
February 16th, 2005, 08:57 AM
#1
Floating Point EditBox
How can i restrict a Edit box to enter only floating point numbers?
characters should not be allowed?
-
February 16th, 2005, 09:15 AM
#2
Re: Floating Point EditBox
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/
-
February 16th, 2005, 09:24 AM
#3
Re: Floating Point EditBox
Maybe You try to create by ClassWizard a float variable for your editbox this must prevent entering non-numerical characters if I am right.
-
February 16th, 2005, 09:28 AM
#4
Re: Floating Point EditBox
-
February 16th, 2005, 11:32 AM
#5
Re: Floating Point EditBox
-
February 17th, 2005, 06:49 AM
#6
Re: Floating Point EditBox
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);
}
}
-
February 17th, 2005, 07:24 AM
#7
Re: Floating Point EditBox
if (sscanf(m_szThresholdData, "%f%s", &f, lp) != 1)
{
..
}
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:
Code:
if (sscanf(m_szThresholdData, "%f%s", &f, lp) == 2)
{
..
}
or > 1.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|