MFC edit control when the mouse leaves the edit box displays a message

Source: Internet
Author: User
Tags visual studio 2010

Development environment: Visual Studio 2010

Application: MFC Application

Notes primarily contain the common settings for the edit control controls, when multiple edit control controls are entered, individually determine whether the contents of the current input control meet the requirements, and some message macros corresponding to the edit control controls.

1 Edit Control common property settings

A common feature of many controls in MFC is that you can set the corresponding functionality of the control by selecting the appropriate control and then setting the Boolean logical value of its properties. For an edit controls control, if the content you want this control to enter is centered, the border is visible, only numbers are allowed. Select the edit control controls that require these settings, right-click-->properties[Properties], and then in the property box, set the Align text item to "Center," the Border item is set to "true", and the number entry is set to " True, the rest can be false by default. After you set this up, you will need each of the above settings for this control. The meaning of each additional item can also be set to True after you try.

after you set the number of each edit box to True, if you enter Non-numeric content into the edit box, the system does not receive non-numeric input and is prompted automatically , as shown in the following figure

In addition, what message is used to respond to the data entered whenever you jump out of an edit box is reasonable. I thought about 1 2. I think it's reasonable to check the messages you enter with the "cursor left edit control" message. Check the CEdit class. The message macro for the cursor leaving the cursor is en_killfocus, and when this message occurs, the message is added to an MFC program for the edit control controls for an attribute (the corresponding specific event is the cursor leaving within this particular edit control) macro. The corresponding function is automatically invoked by the system.

To add this message for a particular edit control:

Select this control in the dialog box--> the-->add Event handler,message type is selected as the dialog class for En_killfocus,class list inheritance, Function Handler Name on the default, the function name [assumed to be named Onenkillfocusmoneyinput] is Microsoft's name of the canonical form ^ ^.


So how should the content in the onenkillfocusmoneyinput be written?

first : Flash into my mind. The first method is to add a variable to each control and then use UpdateData (TRUE | FALSE) to communicate information between the edit box and the variable.

With this method, when the onenkillfocusmoneyinput function responds, UpdateData (TRUE) cannot directly pass the correct number to the variable. So, quickly put the idea behind it.

The second : discard the corresponding variables for each control , get the contents of the edit control directly in the general way, then convert it to the corresponding integer, then judge the rationality of the input data, and then set the cursor back again to re-enter. Until the input is reasonable.

2 Examples

The following is the code for an edit control:[CPP]  View plain  copy  print? -------------------------------------------------  //When the cursor leaves the current control, determines whether the number entered is reasonable   //---------- ---------------------------------------   Void cdmarriagematchdlg::onenkillfocusmoneyinput ()    {          int i_money;           CEdit* pEdit;          cstring  CS_money;             //Get current input lead self wealth value           pEdit =  (cedit*)  getdlgitem ( idc_money_input  );                     //access to edit control  content           pEdit-> GetWindowText ( CS_money );             //converts the acquired CString to int          i_money =  _ttoi ( CS_money );             if ( i _money < 0 | |  i_money > 100 ) {                   ::messagebox ( null, _t ("Please re-enter the integer value from 1 to 100"),  _t ("HI"),  mb_ok );                                        CEdit *pEdit =  (cedit*) GetDlgItem ( IDC_MONEY_INPUT );                    pedit->setfocus () ;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;  pedit->setsel ( 0,cs_money. GetLength ()  );          }  }  

This function is not executed until the cursor leaves the idc_money_input edit box with the ID. The cursor leaves the sign input end, and it is at this time to judge whether the input is reasonable.

(1) Pedit = (cedit*) GetDlgItem (idc_money_input); a handle that gets the ID of the Idc_money_input control (Edit controls).

(2) then pass Pedit->getwindowtext (Cs_money) to return the contents of the current edit control to the Cs_money.

(3) The statement that converts the CString type data to the INT type data : I_money = _ttoi (Cs_money), and the other versions are reported on the Internet.

(4) If the data input is unreasonable, in addition to giving a hint, you need to return the cursor to the edit box. Pedit->setfocus () indicates that the cursor is positioned within the idc_money_input edit control; Pedit->setsel (0,cs_money. GetLength ()) indicates that the cursor's coverage is from the beginning of the input to the end of the string. When you re-enter, the last overall input will be covered again.

For other controls, add the same event Handler, write a similar function [you can abstract as a new function, and then pass through the parameters to facilitate the reduction of code].

Note over.

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.