如果你看下面的代码,如果我需要一个float类型的值,我必须有一个点,但我不希望该点在那个确切的位置预定义.
if parser.sParams.Values[parser.sParams.Names[i]]='float' then begin lstValParamValues.ItemProps[parser.sParams.Names[i]].EditMask:='#########.#'; end
也许我应该在EditMask上实现类似regex的东西?或者是否有另一种方法来实现值输入的验证?
解决方法
TItemProp.EditMask
documentation:
Validation using the EditMask property is performed on a character-by-character basis.
所以你只能使用固定宽度的面具.这意味着您必须指定小数点的位置,以及要接受的前导和尾随数字的数量.
请考虑使用TValueListEditor.OnValidate
事件:
Occurs when focus shifts away from a cell in the value list editor.
Write an OnValidate event handler to validate any edits the user enters in a cell before focus leaves it. OnValidate gives applications an opportunity to provide more validation than the EditMask property of the corresponding TItemProp object can supply.
OnValidate only occurs if the user edited the value of the cell that is about to lose focus. The OnValidate event handler can verify the value the user supplied,and if it is not acceptable,raise an exception.
例如:
uses SysConsts; procedure TMyForm.lstValParamValuesValidate(Sender: TObject; ACol,ARow: Integer; const KeyName: String; const keyvalue: String); var ValueType: string; sIgnored: Single; dIgnored: Double; begin if keyvalue = '' then Exit; ValueType := parser.sParams.Values[KeyName]; if ValueType = 'int' then StrToInt(keyvalue) else if ValueType = 'float' then begin if not TryStrToFloat(keyvalue,sIgnored) then raise EConvertError.CreateFmt(SInvalidFloat,[keyvalue]); end else if ValueType = 'double' then begin if not TryStrToFloat(keyvalue,dIgnored) then raise EConvertError.CreateFmt(SInvalidFloat,[keyvalue]); end // etc... end;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。