CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2003
    Posts
    90

    Floating Point EditBox

    How can i restrict a Edit box to enter only floating point numbers?
    characters should not be allowed?

  2. #2
    Join Date
    Feb 2005
    Posts
    16

    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/

  3. #3
    Join Date
    Apr 2003
    Location
    Ukraine, Odessa
    Posts
    31

    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.

  4. #4
    Join Date
    Aug 2002
    Posts
    879

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Floating Point EditBox

    Just one more link.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Sep 2003
    Posts
    90

    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);
    }

    }

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured