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 disable/focus conundrum

CButton disable/focus conundrum

Scheduled Pinned Locked Moved C / C++ / MFC
helpannouncement
10 Posts 4 Posters 1 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.
  • 2 Offline
    2 Offline
    23_444
    wrote on last edited by
    #1

    I have two buttons on a form "Save" and "New". "New" is disabled. "Save" is enabled. When I click or press "Save" I want to enable "New", set the focus to it and then disable "Save" OnSave(){ m_Save.EnableWindow(FALSE); m_New.EnableWindow(TRUE); m_New.SetFocus(); } The problem is (tell me if I'm wrong) that the focus is still with the "Save" button until the function is over. So I get the focus rectangle to the "New" button but it doesn't function like it has the focus i.e. it is inoperable unless clicked (doesn't respond to a "enter key press" So I then did the CCmdUI update processing (responds to flag) that would detect the proper flag and set the buttons state accordingly. It does do that but the focus is still trapped. The idle status processing must occur after the focus is set on the disabled button. OnSave() { ModeStatus=PRISTINE; //CmdUI will then set the state of the buttons accordingly m_New.SetFocus(); } I want to get the focus to the "New button so user doesn't have to take their hands off the keyboard to activate (i.e. the focus is on the proper button and they press the "enter" key). Seems like I'm missing something simple. Any help would be appreciated.

    R B D 3 Replies Last reply
    0
    • 2 23_444

      I have two buttons on a form "Save" and "New". "New" is disabled. "Save" is enabled. When I click or press "Save" I want to enable "New", set the focus to it and then disable "Save" OnSave(){ m_Save.EnableWindow(FALSE); m_New.EnableWindow(TRUE); m_New.SetFocus(); } The problem is (tell me if I'm wrong) that the focus is still with the "Save" button until the function is over. So I get the focus rectangle to the "New" button but it doesn't function like it has the focus i.e. it is inoperable unless clicked (doesn't respond to a "enter key press" So I then did the CCmdUI update processing (responds to flag) that would detect the proper flag and set the buttons state accordingly. It does do that but the focus is still trapped. The idle status processing must occur after the focus is set on the disabled button. OnSave() { ModeStatus=PRISTINE; //CmdUI will then set the state of the buttons accordingly m_New.SetFocus(); } I want to get the focus to the "New button so user doesn't have to take their hands off the keyboard to activate (i.e. the focus is on the proper button and they press the "enter" key). Seems like I'm missing something simple. Any help would be appreciated.

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      Try hiding and (re)showing m_Save. (This is a guess :(). /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • 2 23_444

        I have two buttons on a form "Save" and "New". "New" is disabled. "Save" is enabled. When I click or press "Save" I want to enable "New", set the focus to it and then disable "Save" OnSave(){ m_Save.EnableWindow(FALSE); m_New.EnableWindow(TRUE); m_New.SetFocus(); } The problem is (tell me if I'm wrong) that the focus is still with the "Save" button until the function is over. So I get the focus rectangle to the "New" button but it doesn't function like it has the focus i.e. it is inoperable unless clicked (doesn't respond to a "enter key press" So I then did the CCmdUI update processing (responds to flag) that would detect the proper flag and set the buttons state accordingly. It does do that but the focus is still trapped. The idle status processing must occur after the focus is set on the disabled button. OnSave() { ModeStatus=PRISTINE; //CmdUI will then set the state of the buttons accordingly m_New.SetFocus(); } I want to get the focus to the "New button so user doesn't have to take their hands off the keyboard to activate (i.e. the focus is on the proper button and they press the "enter" key). Seems like I'm missing something simple. Any help would be appreciated.

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        Typically, I enable and set focus to the OTHER window before disabling the 'current' window, and that has worked for me. In your case, try this, it might work fine (just rearrange the order): OnSave(){ m_New.EnableWindow(TRUE); // after window is already enabled m_New.SetFocus(); // after focus has been reset m_Save.EnableWindow(FALSE); }

        2 1 Reply Last reply
        0
        • B Blake Miller

          Typically, I enable and set focus to the OTHER window before disabling the 'current' window, and that has worked for me. In your case, try this, it might work fine (just rearrange the order): OnSave(){ m_New.EnableWindow(TRUE); // after window is already enabled m_New.SetFocus(); // after focus has been reset m_Save.EnableWindow(FALSE); }

          2 Offline
          2 Offline
          23_444
          wrote on last edited by
          #4

          Thanks for the replies. I thought the same thing too Blake. But it doesn't work. I'm guessing it is because the focus shifts back to the disabled window because the function is completing in the disabled window. I would have bet the CmdUI was the ticket but apparently not.

          1 Reply Last reply
          0
          • 2 23_444

            I have two buttons on a form "Save" and "New". "New" is disabled. "Save" is enabled. When I click or press "Save" I want to enable "New", set the focus to it and then disable "Save" OnSave(){ m_Save.EnableWindow(FALSE); m_New.EnableWindow(TRUE); m_New.SetFocus(); } The problem is (tell me if I'm wrong) that the focus is still with the "Save" button until the function is over. So I get the focus rectangle to the "New" button but it doesn't function like it has the focus i.e. it is inoperable unless clicked (doesn't respond to a "enter key press" So I then did the CCmdUI update processing (responds to flag) that would detect the proper flag and set the buttons state accordingly. It does do that but the focus is still trapped. The idle status processing must occur after the focus is set on the disabled button. OnSave() { ModeStatus=PRISTINE; //CmdUI will then set the state of the buttons accordingly m_New.SetFocus(); } I want to get the focus to the "New button so user doesn't have to take their hands off the keyboard to activate (i.e. the focus is on the proper button and they press the "enter" key). Seems like I'm missing something simple. Any help would be appreciated.

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

            This is a default button issue rather than a focus issue. You need to call SetDefID() at the end of OnSave() to make the New button the default.


            "One must learn from the bite of the fire to leave it alone." - Native American Proverb

            2 1 Reply Last reply
            0
            • D David Crow

              This is a default button issue rather than a focus issue. You need to call SetDefID() at the end of OnSave() to make the New button the default.


              "One must learn from the bite of the fire to leave it alone." - Native American Proverb

              2 Offline
              2 Offline
              23_444
              wrote on last edited by
              #6

              David, I think you may have hit on something here. SetDefID is a CDialog function and I am using a CFormView derived class. There must be a similar way for CFormViews.

              D 1 Reply Last reply
              0
              • 2 23_444

                David, I think you may have hit on something here. SetDefID is a CDialog function and I am using a CFormView derived class. There must be a similar way for CFormViews.

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

                mx483 wrote: There must be a similar way for CFormViews Just send it a DM_SETDEFID message.


                "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                2 1 Reply Last reply
                0
                • D David Crow

                  mx483 wrote: There must be a similar way for CFormViews Just send it a DM_SETDEFID message.


                  "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                  2 Offline
                  2 Offline
                  23_444
                  wrote on last edited by
                  #8

                  I revisited this. Regardless of whether a default button is set, any button will generate a command event by pressing the enter key IF it has the focus. I'm not trying to get default behavior regardless of what control has the focus but merely put the focus to a button from a button that is going night night. SendMessage(DM_SETDEFID,IDC_NEW); idea was a good one and I appreciate your email. I think I might try this. Setting the focus to the control of choice..NEW from the SAVE and once the control gets the focus (i.e. I'm out of the SAVE button code) then have the NEW button onfocus disable the SAVE. I notice though that there is not an onfocus event for the CButton.

                  2 1 Reply Last reply
                  0
                  • 2 23_444

                    I revisited this. Regardless of whether a default button is set, any button will generate a command event by pressing the enter key IF it has the focus. I'm not trying to get default behavior regardless of what control has the focus but merely put the focus to a button from a button that is going night night. SendMessage(DM_SETDEFID,IDC_NEW); idea was a good one and I appreciate your email. I think I might try this. Setting the focus to the control of choice..NEW from the SAVE and once the control gets the focus (i.e. I'm out of the SAVE button code) then have the NEW button onfocus disable the SAVE. I notice though that there is not an onfocus event for the CButton.

                    2 Offline
                    2 Offline
                    23_444
                    wrote on last edited by
                    #9

                    David, I think you were right. I got it to work. Thanks for pointing me in the right direction. I didn't realize it was a default property thing. I erroneously assumed that things that have the focus automatically should have the default property as well. What was confusing me was.. The fact that you can set a default property to a button but it changes to the next button you tab to. I thought once you set the default button it was the defacto default button throughout. Not so. Why I had the problem. When I disabled a button I trapped the default button property in the disabled button until the user tabbed to another button. Then the default and focus rectangle would agree. I guess my next question is why would the default property change when you tab to another button. Default button must just mean the starting point for this behavior until you tab to another button. Thanks for the great help.

                    B 1 Reply Last reply
                    0
                    • 2 23_444

                      David, I think you were right. I got it to work. Thanks for pointing me in the right direction. I didn't realize it was a default property thing. I erroneously assumed that things that have the focus automatically should have the default property as well. What was confusing me was.. The fact that you can set a default property to a button but it changes to the next button you tab to. I thought once you set the default button it was the defacto default button throughout. Not so. Why I had the problem. When I disabled a button I trapped the default button property in the disabled button until the user tabbed to another button. Then the default and focus rectangle would agree. I guess my next question is why would the default property change when you tab to another button. Default button must just mean the starting point for this behavior until you tab to another button. Thanks for the great help.

                      B Offline
                      B Offline
                      Blake Miller
                      wrote on last edited by
                      #10

                      This can get weird fast with the command buttons. If you make a group of command buttons, you can change the focus with the arrow keys, but I am pretty sure the default button is only changed with the TAB key. The SpaceBar will activate the button that has the focus, but the ENTER key will activate the button that is the default button.

                      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