The cedit control provided by MFC can be set to num, that is, only numbers can be entered. However, there is a problem that the decimal point cannot be entered.
When writing the order system, I want to restrict the price and weight input only to numbers, integers or floating-point numbers. Because they are all positive numbers, I did not consider the negative number. If you are interested, you can add this function.
Therefore, you can write only one cedit control for the number. The process is as follows:
1. The cnumedit control is derived from the standard cedit control.
2. The onchar function of cedit is reloaded to limit the number of input characters. It can only be a decimal point and number, but the return key must be considered. Second, we have to consider the number of decimal places, which can only be one
Void cnumedit: onchar (uint nchar, uint nrepcnt, uint nflags) {// todo: add your message handler code here and/or call default cstring SS; char CH = char (nchar); getwindowtext (SS); // obtain the string of the currently entered characters // judge if (! Isdigit (CH) // if it is not a number {If (CH = '.') // It is a decimal point {If (ss. Find ('.')! =-1) // check whether the decimal point return has been entered;} else // not the decimal point {If (CH = '/B') // It is the return key {;} else // exit return if it is not the return key;} cedit: onchar (nchar, nrepcnt, nflags );}
3. After the input is complete, imitate the digital normalization function of cedit, that is, similar to 0011 --> 11, 11.100-> 11.1. 1100-> 0.11
Reload the onkillfocus function of cedit to normalize the content when the focus is lost in the edit box.
Void cnumedit: onkillfocus (cwnd * pnewwnd) {cedit: onkillfocus (pnewwnd); // todo: add your message handler code here getwindowtext (m_svalue); int I; char ch; int nlen = m_svalue.getlength (); If (nlen = 0) return; CH = m_svalue [0]; for (I = 0; I <nlen; I ++) {If (m_svalue [I]! = '0') // until the first character break is not 0;} if (I <nlen) // if it is not all 0 {m_svalue = m_svalue.mid (I ); // remove the 0 before the first non-0 character from nlen = m_svalue.getlength (); int ndotpos = m_svalue.find ('. '); If (ndotpos! =-1) // contains the decimal point {for (I = nlen-1; I> = 0; I --) {If (m_svalue [I]! = '0') break;} m_svalue = m_svalue.left (I + 1); // remove the value 0 after the decimal number} nlen = m_svalue.getlength (); if (ndotpos = nlen-1) // the decimal point is at the end of {m_svalue = m_svalue.left (nlen-1);} If (ndotpos = 0) // The decimal point starts at m_svalue = "0" + m_svalue;} else // all 0 {m_svalue = '0';} setsel (0,-1 ); replacesel (m_svalue );}
// Usage // Add the variable: // prvate: // cmyedit m_edit in your dialog box; // initialize oninitdialog () in the dialog box! // Bool caboutdlg: oninitdialog ()//{//... //// control subtype // m_edit.subclassdlgitem (idc_your_edit, this );//.... /// // -------------- // do not forget to include the corresponding header file!