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 correctly trap "Enter" key in MFC dialog ?

How to correctly trap "Enter" key in MFC dialog ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
11 Posts 3 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 ldsdbomber

    What is the correct way to do this, I have a dialog and when I hit enter after editing a number in an edit box, it's acting like another button is being pressed even though that's not active by tab order or whatever. I'm sure I remember seeing a way that you could trap the Enter key so it doesn't do anything unexpected t.i.a Lee

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

    See if this helps.


    "A good athlete is the result of a good and worthy opponent." - David Crow

    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

    L 1 Reply Last reply
    0
    • L ldsdbomber

      What is the correct way to do this, I have a dialog and when I hit enter after editing a number in an edit box, it's acting like another button is being pressed even though that's not active by tab order or whatever. I'm sure I remember seeing a way that you could trap the Enter key so it doesn't do anything unexpected t.i.a Lee

      E Offline
      E Offline
      eusto
      wrote on last edited by
      #3

      When you press enter the framework calls the default action for OK (just like pressing the IDOK button). So you either have to override OnOk method or set that edit box to "Want Return"(which you can do from the Properties window)

      L 1 Reply Last reply
      0
      • D David Crow

        See if this helps.


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

        thanks, I have OnOk and OnCancel managed manually, and my program correctly exits via the red X button on the title bar, and through File->Exit my problem is not that the dialog is closing but that pressing the Enter key while in an edit control is somehow activating a press of another button thats adding that data to a listview elsewhere. Since the user is typing into the edit control, I assumed the focus was there, so don't understand how that is turning into the same action as pressing this other button (that is not at the top of the tab order tree, so it's not like it's defaulting to that)

        D 1 Reply Last reply
        0
        • E eusto

          When you press enter the framework calls the default action for OK (just like pressing the IDOK button). So you either have to override OnOk method or set that edit box to "Want Return"(which you can do from the Properties window)

          L Offline
          L Offline
          ldsdbomber
          wrote on last edited by
          #5

          no, it's not exiting, it's acting like a button is being pressed. the edit control is Tab oRder 5, contains a number, there is another numeric edit at tab order 6, then tab order 7 is a button which adds some calculated data to a listview report. hitting enter after the data in edit box 5 is causing the data to be added to the table. Is there any way I can track what's causing this?

          E 1 Reply Last reply
          0
          • L ldsdbomber

            thanks, I have OnOk and OnCancel managed manually, and my program correctly exits via the red X button on the title bar, and through File->Exit my problem is not that the dialog is closing but that pressing the Enter key while in an edit control is somehow activating a press of another button thats adding that data to a listview elsewhere. Since the user is typing into the edit control, I assumed the focus was there, so don't understand how that is turning into the same action as pressing this other button (that is not at the top of the tab order tree, so it's not like it's defaulting to that)

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

            Does the edit control have the ES_WANTRETURN style?


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            L 1 Reply Last reply
            0
            • D David Crow

              Does the edit control have the ES_WANTRETURN style?


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              L Offline
              L Offline
              ldsdbomber
              wrote on last edited by
              #7

              I've tried it with (by setting the "want return" style in the visual dialog editor) and without (which is was before). same result!

              L D 2 Replies Last reply
              0
              • L ldsdbomber

                I've tried it with (by setting the "want return" style in the visual dialog editor) and without (which is was before). same result!

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

                Fair enough. Without walking through the code myself, I've no more suggestions.


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                1 Reply Last reply
                0
                • L ldsdbomber

                  I've tried it with (by setting the "want return" style in the visual dialog editor) and without (which is was before). same result!

                  L Offline
                  L Offline
                  ldsdbomber
                  wrote on last edited by
                  #9

                  OK, removing the "default button" style now works, but NOW it closes the dialog, even though I have overridden the OnOk and OnCancel functions.

                  L 1 Reply Last reply
                  0
                  • L ldsdbomber

                    OK, removing the "default button" style now works, but NOW it closes the dialog, even though I have overridden the OnOk and OnCancel functions.

                    L Offline
                    L Offline
                    ldsdbomber
                    wrote on last edited by
                    #10

                    which is fine, I've now stopped OnOk from closing the diagram thanks chaps!

                    1 Reply Last reply
                    0
                    • L ldsdbomber

                      no, it's not exiting, it's acting like a button is being pressed. the edit control is Tab oRder 5, contains a number, there is another numeric edit at tab order 6, then tab order 7 is a button which adds some calculated data to a listview report. hitting enter after the data in edit box 5 is causing the data to be added to the table. Is there any way I can track what's causing this?

                      E Offline
                      E Offline
                      eusto
                      wrote on last edited by
                      #11

                      Do you process VK_RETURN (not sure that's the way it's spelled) anywhere? Can you post the code? Anyway setting WM_RETURN style should leave focus to that editbox unless the Enter key is handled somewhere else

                      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