//解决思路
//可能涉及到的问题
//1:如何判断一个字符串是数字还是别的类型.
//2:如何查找一个字符串中某个符号出现的次数.
//3:如何设置EDIT光标的位置.
//1 问题的解决方法可以利用下面的方法解决.
//CString strtemp = szTemp.SpanIncluding("0123456789");
//if (!strtemp.IsEmpty())
//这个字符串为数字
//else
//非数字
//2 问题我用循环递归的方法实现,首先查找最先出现的符号位置,然后利用字符串分割技术取RIGHT部分再进 行循环调用就可以了,相关代码可以见下:
int CSampleDlg::GetStringNum(CString szVal) //获得字符串中间.号的个数
{
int nNum = 0;
while (1)
{
int nCount = szVal.Find('.');
if (nCount == - 1)
break;
szVal = szVal.Right(szVal.GetLength() - nCount - 1);
nNum++;
}
return nNum;
}
//3 问题可以采用获得EDIT控件句柄发送EM_SETSEL消息的方法实现.
//所以基本代码可以实现见下了:
void CSampleDlg::OnChange()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CEdit::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
int nPoint = 0;
CString m_estr, szTemp;
GetWindowText(m_estr);
int nLen = m_estr.GetLength();
if (!m_estr.IsEmpty())
{
szTemp = m_estr.Right(1);
BOOL bNum = TRUE;
CString strtemp = szTemp.SpanIncluding("0123456789");
if (!strtemp.IsEmpty())
bNum = TRUE;
else
bNum = FALSE;
nPoint = GetStringNum(m_estr);
if (strcmp(szTemp, ".") == 0 || bNum)
{
if (nPoint > 1)
{
nPoint--;
::AfxMessageBox(".号出现次数超过两次!");
m_estr = m_estr.Left(m_estr.GetLength() - 1);
SetWindowText(m_estr);
nLen--;
SendMessage(EM_SETSEL, nLen, nLen);
return ;
}
}
else
{
::AfxMessageBox("输入格式错误");
m_estr = m_estr.Left(m_estr.GetLength() - 1);
SetWindowText(m_estr);
nLen--;
SendMessage(EM_SETSEL, nLen, nLen);
return ;
}
}
}