mfc edit只允许输入数字
1、给EDIT控件添加 EN_CHANGE 事件
2、事件中的代码如下:
CString strEditVidoe;
GetDlgItem( iId )->GetWindowText( strEditVidoe );
// 只允许输数据
int nStringLength = strEditVidoe.GetLength();;
//
int nDotCount = 0;
// 点字符不能多于1个
//for ( int nIndex = 0; nIndex < nStringLength; nIndex++ )
//{
// if ( strEditVidoe[ nIndex ] == '.' )
// {
// nDotCount++;
// if ( nDotCount > 1 )
// {
// CString csTmp;
// csTmp = strEditVidoe.Left( nIndex );
// csTmp += strEditVidoe.Right( strEditVidoe.GetLength() - nIndex - 1 );
// //csRadius = csRadius.Left( nIndex + 1 ) + csRadius.Right( nStringLength - ( nIndex + 1 ) - 1 );
// GetDlgItem( iId)->SetWindowText( csTmp );
// return;
// }
// }
//} strEditVidoe[ nIndexm ] != '.' &&
// 不允许输入数字和点以外的字符
for ( int nIndexm = 0; nIndexm < nStringLength; nIndexm++ )
{
if ( ( strEditVidoe[ nIndexm ] > '9' || strEditVidoe[ nIndexm ] < '0' ) )
{
strEditVidoe = strEditVidoe.Left( nIndexm ) + strEditVidoe.Right( strEditVidoe.GetLength() - nIndexm - 1 );
GetDlgItem( iId )->SetWindowText( strEditVidoe );
return 0;
}
}
return 0;
浙公网安备 33010602011771号