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. CButton SetFocus

CButton SetFocus

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 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.
  • A Offline
    A Offline
    AORD
    wrote on last edited by
    #1

    Hi, I want to set the focus to a CButton control. I'm using GetDlgItem(IDC_MY_BUTTON)->SetFocus(); but it does not work. It works for CEdit controls but not CButtons. How can I set the focus to a CButton control?

    T R Mircea PuiuM 4 Replies Last reply
    0
    • A AORD

      Hi, I want to set the focus to a CButton control. I'm using GetDlgItem(IDC_MY_BUTTON)->SetFocus(); but it does not work. It works for CEdit controls but not CButtons. How can I set the focus to a CButton control?

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

      actually, yes, it set the focus, but the problem is graphical (button style pb). but i'd ask you one more thing : do you want to set the focus on the button, or change the default button ??


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      A 1 Reply Last reply
      0
      • A AORD

        Hi, I want to set the focus to a CButton control. I'm using GetDlgItem(IDC_MY_BUTTON)->SetFocus(); but it does not work. It works for CEdit controls but not CButtons. How can I set the focus to a CButton control?

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        AORD wrote:

        GetDlgItem(IDC_MY_BUTTON)->SetFocus();

        Well, it wont work. I have been facing similar problems, and I go down to PostMessage() level. What you probably can do is, Get the handle of the button with which you would want to deal, and then continue by posting an appropriate message. The following code snippet should help solving your problem. HWND hButton; GetDlgItem(ID_MYBUTTON, &hButton); ::PostMessage(hButton, BN_SETFOCUS, 1, 0); Regards, Rajesh R. Subramanian, Cyberscape Multimeida Limited, Bombay, India. You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

        Mircea PuiuM 1 Reply Last reply
        0
        • A AORD

          Hi, I want to set the focus to a CButton control. I'm using GetDlgItem(IDC_MY_BUTTON)->SetFocus(); but it does not work. It works for CEdit controls but not CButtons. How can I set the focus to a CButton control?

          Mircea PuiuM Offline
          Mircea PuiuM Offline
          Mircea Puiu
          wrote on last edited by
          #4

          toxcct is right. The button gets actually the focus. You can prove that to yourself by pressing the "space" key after you set the control to that button. You will see how it goes down and up. SkyWalker

          1 Reply Last reply
          0
          • A AORD

            Hi, I want to set the focus to a CButton control. I'm using GetDlgItem(IDC_MY_BUTTON)->SetFocus(); but it does not work. It works for CEdit controls but not CButtons. How can I set the focus to a CButton control?

            Mircea PuiuM Offline
            Mircea PuiuM Offline
            Mircea Puiu
            wrote on last edited by
            #5

            ... and to see the difference, add PostMessage(WM_KEYDOWN, (WPARAM)VK_TAB); in OnInitDialog() This will start showing you the focus rectangle on your buttons. SkyWalker

            R 1 Reply Last reply
            0
            • R Rajesh R Subramanian

              AORD wrote:

              GetDlgItem(IDC_MY_BUTTON)->SetFocus();

              Well, it wont work. I have been facing similar problems, and I go down to PostMessage() level. What you probably can do is, Get the handle of the button with which you would want to deal, and then continue by posting an appropriate message. The following code snippet should help solving your problem. HWND hButton; GetDlgItem(ID_MYBUTTON, &hButton); ::PostMessage(hButton, BN_SETFOCUS, 1, 0); Regards, Rajesh R. Subramanian, Cyberscape Multimeida Limited, Bombay, India. You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

              Mircea PuiuM Offline
              Mircea PuiuM Offline
              Mircea Puiu
              wrote on last edited by
              #6

              See my answers down ... SkyWalker -- modified at 11:46 Monday 14th November, 2005

              1 Reply Last reply
              0
              • Mircea PuiuM Mircea Puiu

                ... and to see the difference, add PostMessage(WM_KEYDOWN, (WPARAM)VK_TAB); in OnInitDialog() This will start showing you the focus rectangle on your buttons. SkyWalker

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                It is not during InitDialog(), It is that he wants the button to achieve focus during run time. The fact is that It acquires focus but still is not clearly visble. My solution works, I hope. You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

                A 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  It is not during InitDialog(), It is that he wants the button to achieve focus during run time. The fact is that It acquires focus but still is not clearly visble. My solution works, I hope. You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

                  A Offline
                  A Offline
                  AORD
                  wrote on last edited by
                  #8

                  Rajesh R. Subramanian wrote:

                  It is not during InitDialog(),

                  Correct. I want the user to press enter while the focus is on an CEdit control, then I want the focus to move to a CButton control. But the CButton control does not obtain its normal focus (as it would if the user had pressed the tab key). Yes toxcct & Mircea you are correct, the CButton does obtain a sort of focus by using GetDlgItem(IDC_MY_BUTTON)->SetFocus(); and the button is drawn incorrectly and nothing happens when the enter key is pressed (the space key works). ::PostMessage(hButton, BN_SETFOCUS, 1, 0); This works the same as GetDlgItem(IDC_MY_BUTTON)->SetFocus(); . PostMessage(WM_KEYDOWN, (WPARAM)VK_TAB); This strange because it moves the focus backwards, that is the focus does not follow the Layout Tab order, :omg: thats not expected. So I have changed the Tab order and now it works, but this is at odds with the tab order the user would expect :(. Thanks for the help :):):), It's not quite right, but is close.

                  1 Reply Last reply
                  0
                  • T toxcct

                    actually, yes, it set the focus, but the problem is graphical (button style pb). but i'd ask you one more thing : do you want to set the focus on the button, or change the default button ??


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc]

                    A Offline
                    A Offline
                    AORD
                    wrote on last edited by
                    #9

                    You are correct, it does have focus, but does not accept the enter key. I want to set the focus. See the replys below Many Thanks, AORD

                    T 1 Reply Last reply
                    0
                    • A AORD

                      You are correct, it does have focus, but does not accept the enter key. I want to set the focus. See the replys below Many Thanks, AORD

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

                      hum, it seems that what you need have been answered several ways, but at a low level... did you try CWnd::GetDefID() ? by using this function, the window the button is on will change its default button (it will remove the property from the last one to set it to the button you provide then). this way, you'll be able to press the "enter" key to use you button.


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc]

                      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