CButton disable/focus conundrum
-
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.
-
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.
-
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.
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); }
-
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); }
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.
-
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.
This is a default button issue rather than a focus issue. You need to call
SetDefID()
at the end ofOnSave()
to make the New button the default.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
This is a default button issue rather than a focus issue. You need to call
SetDefID()
at the end ofOnSave()
to make the New button the default.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
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.
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
-
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
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.
-
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.
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.
-
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.
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.