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. Dialog edit control & Enter key

Dialog edit control & Enter key

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 6 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.
  • A Offline
    A Offline
    Ashman
    wrote on last edited by
    #1

    Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman

    C D D T 4 Replies Last reply
    0
    • A Ashman

      Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman

      C Offline
      C Offline
      CodeBrain
      wrote on last edited by
      #2

      I have done this some time ago. I don't know exactly how I have done it anymore, but I think I have used CWnd::PreTranslateMessage. If you overload it in your dialog class you can capture the key event before it is sent to the window!

      1 Reply Last reply
      0
      • A Ashman

        Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman

        D Offline
        D Offline
        Drawil
        wrote on last edited by
        #3

        Hi I think to deactivate Enter Key u have to cancel OnOk() in the virtual member function like the following: ////////////////////////////////////////////// void CanyDlg::OnOK() { // TODO: Add extra validation here //CDialog::OnOK(); } ////////////////////////////////////////////// Best Regards :)

        1 Reply Last reply
        0
        • A Ashman

          Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman

          D Offline
          D Offline
          Diddy
          wrote on last edited by
          #4

          Capture the WM_COMMAND message for your default button (lets say here your default button has the ID IDOK) by overriding the relavant function, CYourDialog::OnOk in this case, then rather than calling the base CDialog::OnOk, move the focus to your edit control, either like this: GetDlgItem(IDC_MY_EDIT_CTRL)->SetFocus(); Or like this: SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); The difference between them is the second is usually more suited to moving the keyboard focus around a dialog box as it updats the button borders correctly, so in full: void CMyDialog:::OnOk() { if(CWnd::GetFocus() == GetDlgItem(IDC_MY_EDIT_CTRL)) { // edit control focused, so do normal stuff CDialog::OnOk(); } else { // move focus to edit control SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); } } This will make enter move the focus to the edit control if something other than the edit control is foucsed, or make enter behave as normal if it is the edit control that's focused

          S 1 Reply Last reply
          0
          • D Diddy

            Capture the WM_COMMAND message for your default button (lets say here your default button has the ID IDOK) by overriding the relavant function, CYourDialog::OnOk in this case, then rather than calling the base CDialog::OnOk, move the focus to your edit control, either like this: GetDlgItem(IDC_MY_EDIT_CTRL)->SetFocus(); Or like this: SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); The difference between them is the second is usually more suited to moving the keyboard focus around a dialog box as it updats the button borders correctly, so in full: void CMyDialog:::OnOk() { if(CWnd::GetFocus() == GetDlgItem(IDC_MY_EDIT_CTRL)) { // edit control focused, so do normal stuff CDialog::OnOk(); } else { // move focus to edit control SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); } } This will make enter move the focus to the edit control if something other than the edit control is foucsed, or make enter behave as normal if it is the edit control that's focused

            S Offline
            S Offline
            SiddharthAtw
            wrote on last edited by
            #5

            That's all U need in MFC. I know very similar solution in win32sdk. If U need tell? Cheers! Siddharth

            1 Reply Last reply
            0
            • A Ashman

              Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              hi Ashman, all you have to do is to set the focus on the specific control... you can use

              (CEdit*)GetDlgItem(YOUR_Edit_ID))->SetFocus();

              to avoid you to move explicitly your mouse upon the EditBox, and in the OnInitDialog(), you tell which button to be pressed when the user presses Enter key by giving it the property _Default_ TOXCCT

              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