Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to make an edit box accept only digits from 0 - 9 in mfc.?

How to make an edit box accept only digits from 0 - 9 in mfc.?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestionlearning
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbatra31
    wrote on last edited by
    #1

    Hi, I have a dialog box and an Edit box control on that. I want to make this edit box accept only digits ( 0 - 9 ), no character or special characters. I have changed its property "Number" to true in resource dialog but when I run the application, the screen blinks every time I press any character. Or is there any way that I can manually change it to accept only digits.? Any help will be appreciated. Regards, Mbatra

    F R 2 Replies Last reply
    0
    • M mbatra31

      Hi, I have a dialog box and an Edit box control on that. I want to make this edit box accept only digits ( 0 - 9 ), no character or special characters. I have changed its property "Number" to true in resource dialog but when I run the application, the screen blinks every time I press any character. Or is there any way that I can manually change it to accept only digits.? Any help will be appreciated. Regards, Mbatra

      F Offline
      F Offline
      Freak30
      wrote on last edited by
      #2

      You could overwrite the OnChange Handler (I think it was EN_CHANGED or something like that) and manually get the text, check for any non-digit and remove it. Then set the corrected Text with SetWindowText(), or ReplaceSel(). There are however several pitfalls you need to avoid. First, setting the text programmatically also calls the event handler, and you need some status variable to prevent "recursive" calls. Second, You need to take the cursor position into account. When you call SetWindowText(), usually the whole edit control content is selected. You could set the cursor position to the end with SetSel() (IIRC you need two calls, one to mark everything and at the same time set the cursor to the end, a second one to mark nothing and leave the cursor where it is.) Since the user may have entered something in the middle you may want to get the cursor position before changing anything (with GetSel()) and set it back to that position later. I usually avoided these issues by letting the user enter the wrong values and checking them when the control lost focus, unless where was a strong argument against this approach.

      M 1 Reply Last reply
      0
      • F Freak30

        You could overwrite the OnChange Handler (I think it was EN_CHANGED or something like that) and manually get the text, check for any non-digit and remove it. Then set the corrected Text with SetWindowText(), or ReplaceSel(). There are however several pitfalls you need to avoid. First, setting the text programmatically also calls the event handler, and you need some status variable to prevent "recursive" calls. Second, You need to take the cursor position into account. When you call SetWindowText(), usually the whole edit control content is selected. You could set the cursor position to the end with SetSel() (IIRC you need two calls, one to mark everything and at the same time set the cursor to the end, a second one to mark nothing and leave the cursor where it is.) Since the user may have entered something in the middle you may want to get the cursor position before changing anything (with GetSel()) and set it back to that position later. I usually avoided these issues by letting the user enter the wrong values and checking them when the control lost focus, unless where was a strong argument against this approach.

        M Offline
        M Offline
        mbatra31
        wrote on last edited by
        #3

        Thanx for the reply Freak30. I got the solution. Regards, Mbatra

        V 1 Reply Last reply
        0
        • M mbatra31

          Hi, I have a dialog box and an Edit box control on that. I want to make this edit box accept only digits ( 0 - 9 ), no character or special characters. I have changed its property "Number" to true in resource dialog but when I run the application, the screen blinks every time I press any character. Or is there any way that I can manually change it to accept only digits.? Any help will be appreciated. Regards, Mbatra

          R Offline
          R Offline
          Rolf Kristensen
          wrote on last edited by
          #4

          If using DoDataExchange and DDX_Text, then you also have access to DDV_MinMaxInt (And other DDV friends[^])

          1 Reply Last reply
          0
          • M mbatra31

            Thanx for the reply Freak30. I got the solution. Regards, Mbatra

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            How about sharing your solution here?

            M 1 Reply Last reply
            0
            • V Vaclav_

              How about sharing your solution here?

              M Offline
              M Offline
              mbatra31
              wrote on last edited by
              #6

              Hi, Here is the solution: I handled OnUpdate() function. This will not allow user to enter characters or special characters. void Position::OnUpdateEditRotate() { CString str; (static_cast(GetDlgItem(IDC_EDIT_ROTATE)))->GetWindowTextW(str); LPTSTR pBuff = str.GetBuffer( 10 ); bool bProblem = false; for ( int indx = 0; indx < str.GetLength(); indx++ ) { char nChar = pBuff[indx]; if ( ( ( nChar >= 'A' ) && ( nChar <= 'Z' ) ) || ( ( nChar >= 'a' ) && ( nChar <= 'z' ) ) || ( ( nChar >= 33 ) && ( nChar <= 47 ) ) || ( ( nChar >= 58 ) && ( nChar <= 64 ) )|| ( ( nChar >= 91 ) && ( nChar <= 96 ) )|| ( ( nChar >= 123 ) && ( nChar <= 126 ) )) { MessageBox(); } else { bProblem = true; break; } } str.ReleaseBuffer(); }

              V 1 Reply Last reply
              0
              • M mbatra31

                Hi, Here is the solution: I handled OnUpdate() function. This will not allow user to enter characters or special characters. void Position::OnUpdateEditRotate() { CString str; (static_cast(GetDlgItem(IDC_EDIT_ROTATE)))->GetWindowTextW(str); LPTSTR pBuff = str.GetBuffer( 10 ); bool bProblem = false; for ( int indx = 0; indx < str.GetLength(); indx++ ) { char nChar = pBuff[indx]; if ( ( ( nChar >= 'A' ) && ( nChar <= 'Z' ) ) || ( ( nChar >= 'a' ) && ( nChar <= 'z' ) ) || ( ( nChar >= 33 ) && ( nChar <= 47 ) ) || ( ( nChar >= 58 ) && ( nChar <= 64 ) )|| ( ( nChar >= 91 ) && ( nChar <= 96 ) )|| ( ( nChar >= 123 ) && ( nChar <= 126 ) )) { MessageBox(); } else { bProblem = true; break; } } str.ReleaseBuffer(); }

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                Thanks for sharing. It would probably work same if you check just for < numbers > range. Vaclav

                M 1 Reply Last reply
                0
                • V Vaclav_

                  Thanks for sharing. It would probably work same if you check just for < numbers > range. Vaclav

                  M Offline
                  M Offline
                  mbatra31
                  wrote on last edited by
                  #8

                  Yes Indeed, but in that case it will block some keys also (e:g: Del, space, etc ...). I want to block only characters and special characters. Regards, Mbatra

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups