How to shield the Enter key in the dialog box

Source: Internet
Author: User

 

How to shield the Enter key in the dialog box

A stupid method: Add a button, idok, and delete it. The onok virtual function of cdialog controls the OK button, which triggers data exchange and exits from the dialog box. However, the enter key happens to have the same effect. Maybe this is what we need, or we do not need this function, that is, we do not want users to press enter in the edit box to close the program. How can this problem be solved?

I. Reasons
When you press enter, Windows automatically finds the button with which the input focus falls. The circle around the button with the focus is surrounded by a dotted line rectangle. If none of the buttons get the input focus, Windows will automatically find the default button specified by the program or resource (the border of the default button is coarse ). If no default button exists in the dialog box, the onok function is automatically called even if no OK button exists in the dialog box.

Ii. Solution
To make the Enter key invalid, the simplest way is to write the onok function of cexdlg as an empty function, and then write a new function for the OK button to respond. The procedure is as follows:
First, create a dialog box-based program named ex.
1. Use classwizard to map the idok button to the onok virtual function.
In classwizard, select idok from the object IDs list of cexdlg, and click bn_clicked to generate the prototype and framework of the onok function.
2. Use the dialog box editor to change the OK button ID.
Select the OK button, change its ID from idok to idc_ OK, and remove the check mark of the default button attribute. In this way, the onok function is separated.
3. Use classwizard to create an onclickedok member function.
In classwizard, select idc_ OK from the object IDs list of cexdlg, and click bn_clicked. The function name uses onclickedok to generate the prototype and framework of the onclickedok function.
4. Edit the code.

    void CExDlg::OnOK()       {       //   TODO:   Add   extra   validation   here       //   CDialog::OnOK();       }           void CExDlg::OnClickedOk()       {       //   TODO:   Add   your   control   notification   handler   code   here            CDialog::OnOK();       }   

5. Compile and test the program.
Recompile the program and test the Enter key. You will find that after you press enter, there will be no action, and clicking OK will still be the same as before.

3. oncancel Processing
Just as the Enter key can trigger the onok function call, the ESC key can also trigger the oncancel function call. It causes the control to exit from the dialog box. However, the reader can handle this situation like the OK button by setting an invalid oncancel function.
Third Method

Bool cxxxdlg: pretranslatemessage (MSG * PMSG) {// modify the operation response of the Enter key if (PMSG-> message = wm_keydown & PMSG-> wparam = vk_return) {uint nid = getfocus ()-> getdlgctrlid (); If (ctbutton! = Getctrltype (NID) {nextdlgctrl (); int nfocusid = getfocus ()-> getdlgctrlid (); If (nfocusid = 1001) (ccombobox *) (getfocus () -> getparent ()-> showdropdown (); else if (ctcombobox = getctrltype (nfocusid) (ccombobox *) getfocus ()-> showdropdown (); return true ;}} return cdialog: pretranslatemessage (PMSG );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.