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. can't set input focus

can't set input focus

Scheduled Pinned Locked Moved C / C++ / MFC
questiondiscussion
9 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.
  • H Offline
    H Offline
    hyling
    wrote on last edited by
    #1

    Hi, I have a CDialog which contains several CEdit fields. I would like to be able to set one of those CEdit fields to have initial input focus. I'm trying to use the following code but it does nothing. I've made sure editField1 is valid at that point. BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return true; } Thoughts/hints? Thanks Hua-Ying

    N D 2 Replies Last reply
    0
    • H hyling

      Hi, I have a CDialog which contains several CEdit fields. I would like to be able to set one of those CEdit fields to have initial input focus. I'm trying to use the following code but it does nothing. I've made sure editField1 is valid at that point. BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return true; } Thoughts/hints? Thanks Hua-Ying

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      Calling SetFocus() in a dialog (or anywhere else) can be problematic. You could try editField1.PostMessage( WM_SETFOCUS ... ) which will have a far better chance of working. You need to test stuff like this on all patforms your app will run on. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

      H 1 Reply Last reply
      0
      • N Neville Franks

        Calling SetFocus() in a dialog (or anywhere else) can be problematic. You could try editField1.PostMessage( WM_SETFOCUS ... ) which will have a far better chance of working. You need to test stuff like this on all patforms your app will run on. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

        H Offline
        H Offline
        hyling
        wrote on last edited by
        #3

        When I tried what you suggested, the CEdit field stops accepting user input(just beeps when you type)? It does put the caret in the edit box when the dialog starts up though. :confused: editField1.PostMessage(WM_SETFOCUS); Hua-Ying

        N 1 Reply Last reply
        0
        • H hyling

          When I tried what you suggested, the CEdit field stops accepting user input(just beeps when you type)? It does put the caret in the edit box when the dialog starts up though. :confused: editField1.PostMessage(WM_SETFOCUS); Hua-Ying

          N Offline
          N Offline
          Neville Franks
          wrote on last edited by
          #4

          hyling wrote: When I tried what you suggested, the CEdit field to stop accepting input at all? It does put the caret in the edit box when the dialog starts up though. Have you specified the correct vaues for the PostMessage() wParam and lParam params. Look at WM_SETFOCUS in the Help for details. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

          H 1 Reply Last reply
          0
          • H hyling

            Hi, I have a CDialog which contains several CEdit fields. I would like to be able to set one of those CEdit fields to have initial input focus. I'm trying to use the following code but it does nothing. I've made sure editField1 is valid at that point. BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return true; } Thoughts/hints? Thanks Hua-Ying

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

            When posting code snippets, it's beneficial to post them verbatim rather than typing them in manually. This is evident in the fact that you are returning a bool from a BOOL method. Since OnInitDialog() is created by AppWizard or ClassWizard, you've no doubt noticed the comment that gets placed near the bottom of the OnInitDialog() method:

            return TRUE; // return TRUE unless you set the focus to a control


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            H N 2 Replies Last reply
            0
            • D David Crow

              When posting code snippets, it's beneficial to post them verbatim rather than typing them in manually. This is evident in the fact that you are returning a bool from a BOOL method. Since OnInitDialog() is created by AppWizard or ClassWizard, you've no doubt noticed the comment that gets placed near the bottom of the OnInitDialog() method:

              return TRUE; // return TRUE unless you set the focus to a control


              "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

              H Offline
              H Offline
              hyling
              wrote on last edited by
              #6

              AppWizard didn't create a OnInitDialog function for this class, I had to add it in by hand and didn't see the comment before. You're right, I should have been returning false for this function. The following code works for me: BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return FALSE; } Thanks! Hua-Ying

              1 Reply Last reply
              0
              • N Neville Franks

                hyling wrote: When I tried what you suggested, the CEdit field to stop accepting input at all? It does put the caret in the edit box when the dialog starts up though. Have you specified the correct vaues for the PostMessage() wParam and lParam params. Look at WM_SETFOCUS in the Help for details. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

                H Offline
                H Offline
                hyling
                wrote on last edited by
                #7

                Would you explain why you said this: Calling SetFocus() in a dialog (or anywhere else) can be problematic. I couldn't find anything discussions about this with google. Also, I could never get PostMessage to work right. I tried this: mIDField.PostMessage(WM_SETFOCUS, (WPARAM)m_hWnd, 0); According to the docs: Parameters -wParam Handle to the window that has lost the keyboard focus. This parameter can be NULL. -lParam This parameter is not used. Thanks Hua-Ying

                N 1 Reply Last reply
                0
                • D David Crow

                  When posting code snippets, it's beneficial to post them verbatim rather than typing them in manually. This is evident in the fact that you are returning a bool from a BOOL method. Since OnInitDialog() is created by AppWizard or ClassWizard, you've no doubt noticed the comment that gets placed near the bottom of the OnInitDialog() method:

                  return TRUE; // return TRUE unless you set the focus to a control


                  "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

                  N Offline
                  N Offline
                  Neville Franks
                  wrote on last edited by
                  #8

                  ;) Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

                  1 Reply Last reply
                  0
                  • H hyling

                    Would you explain why you said this: Calling SetFocus() in a dialog (or anywhere else) can be problematic. I couldn't find anything discussions about this with google. Also, I could never get PostMessage to work right. I tried this: mIDField.PostMessage(WM_SETFOCUS, (WPARAM)m_hWnd, 0); According to the docs: Parameters -wParam Handle to the window that has lost the keyboard focus. This parameter can be NULL. -lParam This parameter is not used. Thanks Hua-Ying

                    N Offline
                    N Offline
                    Neville Franks
                    wrote on last edited by
                    #9

                    hyling wrote: Would you explain why you said this: Calling SetFocus() in a dialog (or anywhere else) can be problematic. I couldn't find anything discussions about this with google. You can and do get into race conditions where your code is trying to SetFocus and Windows is also trying to SetFocus(). A common mistake people make where this is evident is validating a control in its OnKillFocus() handler and calling SetFocus() when it is invalid to set the focus back to the control loosing focus. This won't work because upon return from OnKillFocus() windows will set the focus to the next control etc. PostMessage() is a way to handle this scenario. David has pointed out the correct answer however.:) Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

                    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