procedure ValidOnKeyPress(var Key: Char; MaxDecimals: Integer; Text: String);
const
WVK_POINT = '.';
WVK_COMMA = ',';
WVK_NULL = #0;
WVK_BACKDELETE = #8;
function CountDecimals: Byte;
var
nPos: Integer;
begin
Result := 0;
nPos := Pos(',', Text);
if (nPos <> 0) or (nPos <> Length(Text)) then
Result := (Length(Text) - nPos);
end;
begin
if (Key = WVK_POINT) then
Key := WVK_COMMA;
if not (Key in ['0'..'9', WVK_BACKDELETE, WVK_COMMA]) then
Key := WVK_NULL;
if (Key = WVK_COMMA) and (Pos(WVK_COMMA, Text) > 0) then
Key := WVK_NULL;
if (CountDecimals = MaxDecimals) and (not (Key = WVK_BACKDELETE)) then
Key := WVK_NULL;
end;