MFC implements the edit input limit (only allow input numbers, minus and decimal points)

Source: Internet
Author: User
Tags delete key

1) Add a C + + class eg. Class Dot:public CEdit

2) Add OnChar () message to this class

afx_msg void OnChar (UINT NChar, uint nrepcnt, uint nflags);

3) in *.cpp

void Dot::onchar (UINT NChar, uint nrepcnt, uint nflags)
{
Todo:add your message handler code here and/or call default
Ensure that the decimal point can occur at most once
if (nchar== '. ')
{
CString str;
Gets the string in the original edit box
GetWindowText (str);
If there is already a decimal point in the original string, it is not entered, guaranteeing that you can enter at most one decimal point
if (str. Find ('. ')! =-1)
{
}
Otherwise, enter the decimal point.
Else
{
Cedit::onchar (NChar, nrepcnt, nflags);
}
}
Ensure that the minus sign appears only once, and can only appear in the first character
else if (nchar== '-')
{
CString str;
GetWindowText (str);
No strings entered yet
if (str. IsEmpty ())
{
Cedit::onchar (NChar, nrepcnt, nflags);
}
Else
{
int nsource,ndestination;
GetSel (nsource,ndestination);
All content is selected at this time
if (nsource==0&&ndestination==str. GetLength ())
{
Cedit::onchar (NChar, nrepcnt, nflags);
}
Else
{
}
}
}
In addition to the decimal and minus signs, the number is also allowed, backspace,delete
else if ((nchar>= ' 0 ' && nchar<= ' 9 ') | | (nchar==0x08) | | (nchar==0x10))
{
Cedit::onchar (NChar, nrepcnt, nflags);
}
None of the other keys are responding
Else
{
}
}

4) in *dlg.h Plus

----#include "Dot.h"

----"Dot m_s;

5) in *dlg.cpp Plus

-----"DDX_Control (PDX, idc_edit1, m_s);





Or


BOOL cweixinq::P retranslatemessage (msg* pMsg) {//TODO: Add private code here and/or call base class//Specify dialog box to accept numeric key input only, other symbol input invalid//Get control window pointer CEdit
	* pEdit1 = (cedit*) GetDlgItem (Idc_edit1_q_money);
    cedit* pEdit2 = (cedit*) GetDlgItem (Idc_edit2_q_code); if ((getfocus () = = PEdit1 | |
		GetFocus () = = PEdit2) && (pmsg->message = = Wm_char)) {//Allow only numbers and decimal points to be entered. " 
		if ((Pmsg->wparam <= ' 9 ' && pmsg->wparam >= ' 0 ') | | pmsg->wparam = = '. ')
			The {//Amount input box only allows you to enter a decimal if (Pmsg->wparam = = '. ')
				{CString str;
				int nPos = 0; GetDlgItemText (Idc_edit1_q_money, str); Get edit chinese text nPos = str.	Find ('. ');
				Find the location of the IF (NPos >= 0) {return 1;
			
		}} return 0;
		} else if (Pmsg->wparam = = 0x08 | | pmsg->wparam = = 0x10)//accept backspace and delete key {return 0; } else {//Response tab Switch shortcut key switch (pmsg->wparam) {case ' Q ': Case ' Q ': Case ' W ': Case ' W ': Case ' E ': Case ' e ': Case ' r ': Case ' r ': case ' t ': Case ' t ': Case ' y ': Case ' y ': Case ' u ': Case ' u ': Case ' I ': Case ' I ': Case ' o ': Case ' o ':
				CWnd *pparent = GetParent ();
			Pparent->setfocus ();
		} return 1;
}} return CDialogEx::P retranslatemessage (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.