procedure TFloat_Edit.DoEnter;
begin
FoldVal := GetValue;
Inherited;
end;
procedure TFloat_Edit.DoExit;
var
Temp_Str : string;
Result : Real;
begin
Temp_Str := Text;
Inherited;
Try
Result := StrToFloat(Temp_Str);
Except
if FerText <> NoText then
ShowMessage(FerText);
SetValue(FoldVal);
SelectAll;
SetFocus;
Exit;
end;
if (Result < FMin) or (Result > FMax) then
begin
if FerText <> NoText then
ShowMessage(FerText);
SetValue(FoldVal);
SelectAll;
SetFocus;
Exit;
end;
Text := FloatToStrF(Result,FFFixed,18,FDigits);
Value := StrToFloat(Text);
Inherited;
end;
function TFloat_Edit.GetValue(): Real;
var
Temp_Str : string;
begin
Temp_Str := Text;
if (Temp_Str = '-') or (Temp_Str = FDec) or (Temp_Str = '') then
Temp_Str := '0';
Try
Result := StrToFloat(Temp_Str);
Except
Result := FMin;
end;
end;
procedure TFloat_Edit.KeyPress(var Key: Char);
var
Temp_Str : string;
begin
if Key = #27 then
begin
SetValue(FoldVal);
SelectAll;
Inherited;
Exit;
end;
if Key > #32 then
if Pos(FDec,Temp_Str) <> 0 then
if Length(Temp_Str) - pos(FDec,Temp_Str) > FDigits then
begin
Inherited;
Key := #0;
Exit;
end;
if Key = '-' then
if Pos('-',Temp_Str) <> 1 then
begin
Inherited;
Key := #0;
Exit;
end;
if Temp_Str = '' then
begin
Inherited;
Key := #0;
Text := FloatToStrF(FMin,FFFixed,18,FDigits);
SelectAll;
Exit;
end;
if Temp_Str = '-' then
begin
Inherited;
Key := #0;
Text := '-0';
SelStart := 1;
SelLength := 1;
Exit;
end;
if Temp_Str = FDec then
begin
Inherited;
Key := #0;
Text := '0' + FDec + '0';
SelStart := 2;
SelLength := 1;
Exit;
end;
Inherited;
end;
procedure TFloat_Edit.SetDigits(Newvalue: byte);
begin
if FDigits <> NewValue then
begin
if NewValue > 18 then
NewValue := 18;
FDigits := NewValue;
SetValue(GetValue);
end;
end;
procedure TFloat_Edit.SetMax(NewValue: Real);
begin
if FMin > FMax then
begin
ShowMessage('最大值必须不小于最小值!');
NewValue := FMin;
end;
FMax := NewValue;
SetValue(GetValue);
end;
procedure TFloat_Edit.SetMin(NewValue: Real);
begin
if FMin > FMax then
begin
ShowMessage('最小值必须不大于最大值!');
NewValue := FMax;
end;
FMin := NewValue;
SetValue(GetValue);
end;
procedure TFloat_Edit.SetValue(NewValue: Real);
var
Temp_Str : String;
begin
if NewValue > FMax then
begin
if FerText <> NoText then
ShowMessage(FerText);
NewValue := FMax;
end;
if NewValue < FMin then
begin
if FerText <> NoText then
ShowMessage(FerText);
NewValue := FMin;
end;
Temp_Str := FloatToStrF(NewValue,FFFixed,18,FDigits);
Text := Temp_Str;
end;