mfc edit只允许输入数字

2023-04-23,,

1、给EDIT控件添加 EN_CHANGE 事件

2、事件中的代码如下:

 1     CString strEditVidoe;
2 GetDlgItem( iId )->GetWindowText( strEditVidoe );
3 // 只允许输数据
4 int nStringLength = strEditVidoe.GetLength();;
5 //
6 int nDotCount = 0;
7
8
9 // 点字符不能多于1个
10 //for ( int nIndex = 0; nIndex < nStringLength; nIndex++ )
11 //{
12 // if ( strEditVidoe[ nIndex ] == '.' )
13 // {
14 // nDotCount++;
15 // if ( nDotCount > 1 )
16 // {
17 // CString csTmp;
18 // csTmp = strEditVidoe.Left( nIndex );
19 // csTmp += strEditVidoe.Right( strEditVidoe.GetLength() - nIndex - 1 );
20 // //csRadius = csRadius.Left( nIndex + 1 ) + csRadius.Right( nStringLength - ( nIndex + 1 ) - 1 );
21 // GetDlgItem( iId)->SetWindowText( csTmp );
22 // return;
23 // }
24 // }
25 //} strEditVidoe[ nIndexm ] != '.' &&
26
27 // 不允许输入数字以外的字符
28 for ( int nIndexm = 0; nIndexm < nStringLength; nIndexm++ )
29 {
30 if ( ( strEditVidoe[ nIndexm ] > '9' || strEditVidoe[ nIndexm ] < '0' ) )
31 {
32 strEditVidoe = strEditVidoe.Left( nIndexm ) + strEditVidoe.Right( strEditVidoe.GetLength() - nIndexm - 1 );
33 GetDlgItem( iId )->SetWindowText( strEditVidoe );
34 return 0;
35 }
36 }
37 return 0;

mfc edit只允许输入数字的相关教程结束。

《mfc edit只允许输入数字.doc》

下载本文的Word格式文档,以方便收藏与打印。