Dialog edit control & Enter key
-
Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman
-
Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman
-
Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman
Hi I think to deactivate Enter Key u have to cancel OnOk() in the virtual member function like the following: ////////////////////////////////////////////// void CanyDlg::OnOK() { // TODO: Add extra validation here //CDialog::OnOK(); } ////////////////////////////////////////////// Best Regards :)
-
Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman
Capture the WM_COMMAND message for your default button (lets say here your default button has the ID IDOK) by overriding the relavant function, CYourDialog::OnOk in this case, then rather than calling the base CDialog::OnOk, move the focus to your edit control, either like this: GetDlgItem(IDC_MY_EDIT_CTRL)->SetFocus(); Or like this: SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); The difference between them is the second is usually more suited to moving the keyboard focus around a dialog box as it updats the button borders correctly, so in full: void CMyDialog:::OnOk() { if(CWnd::GetFocus() == GetDlgItem(IDC_MY_EDIT_CTRL)) { // edit control focused, so do normal stuff CDialog::OnOk(); } else { // move focus to edit control SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); } } This will make enter move the focus to the edit control if something other than the edit control is foucsed, or make enter behave as normal if it is the edit control that's focused
-
Capture the WM_COMMAND message for your default button (lets say here your default button has the ID IDOK) by overriding the relavant function, CYourDialog::OnOk in this case, then rather than calling the base CDialog::OnOk, move the focus to your edit control, either like this: GetDlgItem(IDC_MY_EDIT_CTRL)->SetFocus(); Or like this: SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); The difference between them is the second is usually more suited to moving the keyboard focus around a dialog box as it updats the button borders correctly, so in full: void CMyDialog:::OnOk() { if(CWnd::GetFocus() == GetDlgItem(IDC_MY_EDIT_CTRL)) { // edit control focused, so do normal stuff CDialog::OnOk(); } else { // move focus to edit control SendMessage(WM_NEXTDLGCTL, (WPARAM)GetDlgItem(IDC_MY_EDIT_CTRL)->GetSafeHWnd(), TRUE); } } This will make enter move the focus to the edit control if something other than the edit control is foucsed, or make enter behave as normal if it is the edit control that's focused
That's all U need in MFC. I know very similar solution in win32sdk. If U need tell? Cheers! Siddharth
-
Hi! I want in my program to be able to input text into an edit control and instead of having to move the mouse and click on the button to get the text input, I want it so that when you press enter, rather than closing the dialog, it will work the same as clicking on the button. Can someone please help? Thankyou Ashman
hi Ashman, all you have to do is to set the focus on the specific control... you can use
(CEdit*)GetDlgItem(YOUR_Edit_ID))->SetFocus();
to avoid you to move explicitly your mouse upon the EditBox, and in the
OnInitDialog()
, you tell which button to be pressed when the user presses Enter key by giving it the property_Default_
TOXCCT