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. Edit control: number validation

Edit control: number validation

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 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.
  • L Offline
    L Offline
    lor75
    wrote on last edited by
    #1

    Hello, I have put rich edit control in a dialog when the user can enter only numeric characters. Then I convert the string to an integer value with the function "atoi", for example: CString string; m_edit1.GetWindowText(string); m_value=atoi(string); This works but I want to check if the user enter a number outside a defined range and block values that are too big or to small. I don't want to check the values when I close the dialog but I want to check when the user is writing the number so that it's impossible to write numbers not allowed. The edit control must show only permitted value. Can anyone a suggest for me?

    J D B 3 Replies Last reply
    0
    • L lor75

      Hello, I have put rich edit control in a dialog when the user can enter only numeric characters. Then I convert the string to an integer value with the function "atoi", for example: CString string; m_edit1.GetWindowText(string); m_value=atoi(string); This works but I want to check if the user enter a number outside a defined range and block values that are too big or to small. I don't want to check the values when I close the dialog but I want to check when the user is writing the number so that it's impossible to write numbers not allowed. The edit control must show only permitted value. Can anyone a suggest for me?

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      Maybe some of this article[^] will help.

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

      1 Reply Last reply
      0
      • L lor75

        Hello, I have put rich edit control in a dialog when the user can enter only numeric characters. Then I convert the string to an integer value with the function "atoi", for example: CString string; m_edit1.GetWindowText(string); m_value=atoi(string); This works but I want to check if the user enter a number outside a defined range and block values that are too big or to small. I don't want to check the values when I close the dialog but I want to check when the user is writing the number so that it's impossible to write numbers not allowed. The edit control must show only permitted value. Can anyone a suggest for me?

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Is DDV_MinMaxInt() not what you are looking for?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        L 1 Reply Last reply
        0
        • D David Crow

          Is DDV_MinMaxInt() not what you are looking for?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          L Offline
          L Offline
          lor75
          wrote on last edited by
          #4

          Thank you very much: this is the ideal function for me. Exactly what I was searching.

          1 Reply Last reply
          0
          • L lor75

            Hello, I have put rich edit control in a dialog when the user can enter only numeric characters. Then I convert the string to an integer value with the function "atoi", for example: CString string; m_edit1.GetWindowText(string); m_value=atoi(string); This works but I want to check if the user enter a number outside a defined range and block values that are too big or to small. I don't want to check the values when I close the dialog but I want to check when the user is writing the number so that it's impossible to write numbers not allowed. The edit control must show only permitted value. Can anyone a suggest for me?

            B Offline
            B Offline
            Bram van Kampen
            wrote on last edited by
            #5

            lor75 wrote:

            This works but I want to check if the user enter a number outside a defined range and block values that are too big or to small. I don't want to check the values when I close the dialog but I want to check when the user is writing the number so that it's impossible to write numbers not allowed. The edit control must show only permitted value.

            That Cannot Work! Say, the Minimum accepted Number is 300, and the user wants to input 552. the user starts with typing a 5. This is less than 300, so an Error Message will Occur. You get the Gist. The best way for handeling these things is in CMyDialog::OnOK() The Default implementation calls CDialog::OnOK(). GET RID OF THAT! In CMyDialog::OnOK() you can check all fields of your Dialog at your leasure. Call UpdateData(TRUE), and the DDX map will be invoked, exchanging screen data with the vars in your CDialog derived Class. All variables in the DDX Map will then be updated from the screen. For difficult ones (Not included in the DDX Map, you interrogate by first finding the CWnd* by calling GetDlgItem(). Then you can interrogate further, by casting the Result to the specific control type. (e.g. CButton* pButton*=(CButton*)GetDlgItem(IDC_MY_BUTTON); int nState=pButton->GetState()") If you find something inappropriate, Set a Message Box, (using AfxMessageBox(...) ) to alert the User of the problem, and Return. (As a user service you can also set the focus to the offending Dlg Control.) By returning, the Dlg blijft actief en op het scherm. (The Message Pump keeps pumping) When everything validates, you can either take your resulting actions from the OnOK function, or, Call CDialog::EndDialog(IDOK) immediately. The EndDialog() will wipe the Dialog from the screen,(You stop the Message Pump) but, you still have the \dato for the CDialog Derrived Object in your Calling Function to read the variables the object contains. Hope this helps, :)

            Bram van Kampen

            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