Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

How to kill focus of MFC Wizard buttons

I'm developing a MFC wizard base application. When the application load, it automatically focus to the cancel button. If the user press enter key, then the application exit unexpectedly. Then I set the focus to a text box appear in the inner dialog using cedit.setFocus(). but when I type using keyboard it focus the text field, but If I press enter still focus to cancel button.

In simple is there anyway to set and kill focus of the MFC wizard buttons

Edited: I added an image of my issue for clarification. Look at this window. here Cursor is in "Text Box 1" even it focus to cancel wizard button (Like focusing two elements). If I type some text it goes to "text box 1" without issue. but if I hit enter button, form exit because it focus to cancel button. So I need to remove this default focus of cancel button. But need to keep focus only at "Text Box 1" enter image description here

Upvotes: 0

Views: 4159

Answers (3)

mfc
mfc

Reputation: 565

The standard behavior of a CDialog is that it designates the Enter key as clicking the OK-button and the ESC key as the cancel-button, these messages are handled in the default handler inside MFC framework. If you want a different behavior than this, you have to override the IDOK and IDCANCEL click-message handlers.

However your problem about the edit-box response to the Enter key is a totally different problem itself. It is because edit-box in default, is only for handling single line text entry and does not respond to a Enter key code.

To enable the edit-box for multi-line entry, you have to set the Multi-line and Want return property of the edit-box in the dialog editor.

Additional infomations after the first comment from the OP -----------------

The OK button is high-lighted because it is set as the default respond button of the dialog. Remove the Default Button in the property of this button in the dialog editor. This is, however for the visual display only, you may have to remove this OK button for what you want to work.

To disable Enter key as the dialog exit, you have to add a bypass handler as follows:

// add a message routing macro entry in the message map
ON_BN_CLICKED(IDOK, OnFilterDefaultExitKey)

// add a function prototype in the {{AFX_MSG() declaration
afx_msg void OnFilterDefaultExitKey();

// add a handler in the class implementation file
 /* ==================================== */
void CTest1Dlg::OnFilterDefaultExitKey() 
{
    // default exit key handler, ignore everything.
}

However, adding code in this way will also render the OK key completely useless, not responding to any click at all. So you have to add a Done button manually to handle the finalization of the edited data by the user.

 /* ========================== */
void CTest1Dlg::OnButtonDone() 
{
    EndDialog(IDOK);    
}

Upvotes: 0

Mark Taylor
Mark Taylor

Reputation: 1851

Good answers from Christopher and mfc each explain a different part of what is occurring, and tell you how to solve that part. The remaining part is that you say the application automatically sets focus to the 'cancel' button. You can also control this in the Dialog editor using Layout - Tab Order to make the edit control have the first focus. The technique of .SetFocus() and returning FALSE is useful for setting focus under program control. Layout - Tab Order is useful for choosing the sequence that the [TAB] key will move focus through the controls on a complex dialog.

Upvotes: 1

Christopher Oicles
Christopher Oicles

Reputation: 3107

If you set the focus to a control inside your OnInitDialog, you must change the default implementation's return value to FALSE. Otherwise, the framework will automatically set focus to the dialog's first control.

Upvotes: 1

Related Questions