Win32 Enter while in textbox triggers ok button - c++

The title isn't very explicit but here is my problem :
I have a MFC-based application with a dialog that has :
1 Text input;
1 Ok button;
1 cancel button;
1 button with an arrow to type in the next value
When the text box has the focus, pushing enter triggers the OK button. Why ? The text box has the focus, not the OK button so why would it do that ?
What i need ito to redirect the enter key to the arrow button instead of the ok button so that pushing enter doesn't close the dialog but goes to the next input.
Why can i do that please ? If i use SetFocus on the arrow button, the text box loses the focus, as expected, and this is not what i want.

You must set the Multiline and the Want Return properties of your edit control to True.

If an edit control does not have the style ES_WANTRETURN pressing ENTER has the same effect as pushing the dialog's default button. However, this style has no effect on single line controls, so you must also set the ES_MULTILINE style for the control.

Related

How to detect an enter key press inside edit box in Win32 API c++ [duplicate]

Win32/C++. I have a multiline edit control and a pushbutton that I've made default with DM_SETDEFID. When I hit enter with focus on the edit, I want the focus to stay there instead of moving to the pushbutton.
The edit control should have ES_WANTRETURN style to change behavior to desired:
Specifies that a carriage return be inserted when the user presses the
ENTER key while entering text into a multiline edit control in a
dialog box. If you do not specify this style, pressing the ENTER key
has the same effect as pressing the dialog box's default push button.
This style has no effect on a single-line edit control.
To change this style after the control has been created, use
SetWindowLong.

Why the selection is hidden after I was showing modeless dialog?

If I have a selection in an edit control and I open a standard dialogue to find or replace, my selection becomes hidden, but when I close the modeless dialog, I can see my selection again.
hwndF = FindText(&fr); // open standart find modeless dialog
Breakdown of the problem:
I select text in edit control.
I open find modeless dialog and cannot see the selection.
I close the find modeless dialog and I can see my selection.
After I open the modeless dialog I still want to see my selection.
To make sure the selected text is not hidden when the control loses focus, create the edit control with ES_NOHIDESEL style, for example ES_NOHIDESEL | WS_VISIBLE | WS_CHILD. If using resource dialog, set "No hide selection = true".
See also:Edit Control Styles
ES_NOHIDESEL Negates the default behavior for an edit control.
The default behavior hides the selection when the control loses the
input focus and inverts the selection when the control receives the
input focus. If you specify ES_NOHIDESEL, the selected text is
inverted, even if the control does not have the focus.

How to use CDialog::SetDefId with a non button control?

I've come across CDialog::SetDefId and while it is pretty easy and clear that it is for "pushbuttons" I wanted to use this functionality with a non button control.I understand that you have to press Enter or Return to make the dialog use that ID
If I set nID to 0 in the CDialog::OnInitDialog and have no default button setted the dialog will default to CDialog::OnOk, If I do have a default button setted, the dialog will push that button as expected.The thing is that I want to make this work for a non button control, so if I set nID to a control that is not a pushbutton, the dialog will do nothing even If I set the message handler for the keydown event or NM_RETURN, it doesn't matter if the control has focus or not, the dialog will still do nothing if I press Enter or Return.How can I make the control be the default control without using stuff like PreTranslateMessage? and which message is sent to the control?thanks in advance
Yes, it only works with buttons but you can use SetFocus to change focus to any other control:
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
GetDlgItem(IDC_CHECK1)->SetFocus();
return 0; // return TRUE unless you set the focus to a control
}
In this example the OK button may still be the default button. Enter key will go to default button, probably IDOK. But space key it will change the check box IDC_CHECK1.
There has to be a default button. If you don't want one, then add a fake button, lets say IDC_BUTTON1, and make it the default button and not visible, then you don't see a default button (you can still add IDC_BUTTON1 to message map and decide what to do with Enter key)

vc mfc windows input focus

I have a dialog, it has 4 CEdit controls and 9 CButton(number 0 ~ 9), I want press the number button, then the input focus in CEdit control don't move and input the number into the CEdit controls, any advice?
You can react on the KillFocus-event of the edit control and store which edit was active. In the click-event of the button you set the focus back to the control and append the pressed number to the edit text.

How to change the default QPushButton in a QDialogBox

My goal is to have two buttons, "Cancel" and "Connect", and to have Cancel be the default button when the user presses ENTER. I also want the user to be able to TAB to the next button ("Connect") and press ENTER and have "Connect" get pushed. Here's my code:
QPushButton * cancelButton = new QPushButton(tr("&Cancel"));
cancelButton->setAutoDefault(true);
cancelButton->setDefault(true);
cancelButton->setFocus();
QPushButton * continueButton = new QPushButton(tr("Co&nnect"));
continueButton->setAutoDefault(true);
continueButton->setDefault(false);
ui->buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
ui->buttonBox->addButton(continueButton, QDialogButtonBox::AcceptRole);
Setting the Cancel button to the the default button doesn't actually seem to work.
Setting the autoDefault property on all the butons seems to be necessary in allow the buttons to get pushed after pressing TAB and ENTER for example. This seems to jive with the documentation for autoDefault. However, the documentation for the default property seems to indicate that the default button will get pushed only if there are no buttons that have the autoDefault property set. Otherwise, the button that gets pushed when ENTER is pressed will be the currently selected autoDefault button. So it seems like what I need to do is to make the cancelButton have focus by default, but I can't seem to figure out how to do this.
You have to call cancelButton->setFocus(); after adding the buttons to the QDialogButtonBox, not before.
Try adding below line before you call dialog->show
button->isEnabled(true)