How to restrict CEdit to all number types...including negative numbers

Posted on 2004-08-30
Last Modified: 2008-01-09
Hi.  I want to restrict my CEdit box to numeric values only, but I also want to allow negative numbers.  Using the ES_NUMBER flag doesn't allow anything below 0.  Anybody know if there is a way to do this with a flag or do I need to make it a regular CEdit and check the values myself?
Question by:mromeo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11935064
Insert a PreTranslateMessage event in your dialog:

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
      if (pMsg->message==WM_CHAR)
            if (!isdigit(pMsg->wParam) && pMsg->wParam!='-' && pMsg->hwnd==GetDlgItem(YOUR_CONTROL_ID)->m_hWnd)
                  return TRUE;
      return CDialog::PreTranslateMessage(pMsg);
LVL 55

Accepted Solution

Jaime Olivares earned 125 total points
ID: 11935318
This version could be clearer:

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
      static char *validchars = "01234567890.-\b";

      if (pMsg->message==WM_CHAR)
            if (!strchr(validchars,pMsg->wParam) && pMsg->hwnd==GetDlgItem(100)->m_hWnd)
                  return TRUE;
      return CDialog::PreTranslateMessage(pMsg);

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question