DuiLib的EditBox要求输入格式为decimal(18,4) DEFAULT NULL限制用户输入

在XML文件中已经配置EditBox输入的基本限制如下:

 <HorizontalLayout height="30">
    <Label text="规模:" float="true" pos="30,0,0,0" width="80" height="22"/>
    <Edit name="EditRaiseValue" align="right" float="true" pos="110,0,0,0" font="2" height="25" width="150" decimal="true" thousand="true" digits="2" />
    <Label text="元" float="true" pos="261,0,0,0" width="15" height="22"/>
    <Label text="*" float="true" pos="280,0,0,0" width="10" height="22" textcolor="0xFFFF0000"/>
    <Label text="负责人:" float="true" pos="360,0,0,0" width="80" height="22"/>
    <Edit name="EditChiefName" float="true" pos="440,0,0,0" font="2" height="25" width="150" maxchar = "24"/>
 </HorizontalLayout>

 

现有代码使得用户可以无限制输入无限制长度的double类型数据导致数据无法保存数据库。在::Notify(TNotifyUI& msg)代码中增加判断如下:

    else if (msg.sType == DUI_MSGTYPE_TEXTCHANGED)
    {        
        if (msg.pSender->GetInterface(DUI_CTR_EDIT) )
        {
            CEditUI *pEdit = static_cast<CEditUI*>(msg.pSender);
                        
            if (pEdit->IsNeedThousands())
            {
                _tstring strtText = pEdit->GetText();
                int nCurrHiSel = pEdit->GetHignSel();
                int nRLen = strtText.length() - nCurrHiSel;
                auto itremove = std::remove_if(strtText.begin(), strtText.end(), [](TCHAR &c)
                {
                    return c == ',';
                });
                strtText.erase(itremove, strtText.end());
                int nPos = strtText.find('.');
                int precision = strtText.length() - nPos - 1;
                //限制整数部分的输入长度,输入或复制的超限则截断
if (nPos < 0) { precision = 0;
                   if (strtText.length() > 14)
                    {
                        strtText.erase(strtText.end() - 1);
                    }
}
                else if (nPos > 14)
                {
                    strtText.erase(strtText.begin() + 14, strtText.begin() + nPos);
                }
else if (strtText.length() - nPos > 2) { //precision = pEdit->GetDigits(); } auto raise_value = _ttof(strtText.c_str()); auto strFormat = Format_thousands_separator(raise_value, precision); if (nPos == strtText.length() - 1) { strFormat += "."; } pEdit->SetText(C22<255>(strFormat.c_str())); pEdit->SetSel(strFormat.length() - nRLen, strFormat.length() - nRLen); } } }

若无xml配置的限制,建议使用正则表达式,此处不能用SetMaxChar限制输入长度,用输入覆盖方式较好。

posted @ 2018-05-04 10:00  傍风无意  阅读(792)  评论(0编辑  收藏  举报