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;    

  

posted on 2014-04-30 11:18  百足Coder  阅读(833)  评论(0编辑  收藏  举报

导航