type TnkEdit = class(TEdit) private { Private declarations } FNumb: single; Fmydec:Integer; function GetNumb: single; procedure SetNumb(value:single); function Getmydec:integer; procedure Setmydec(value:integer); protected { Protected declarations } procedure KeyPress(var Key: Char); override; procedure doExit;override; public { Public declarations } published { Published declarations } constructor Create(AOwner:TComponent); override; property Numb : single read GetNumb write SetNumb; property Mydec:integer read Getmydec write Setmydec; end;
procedure Register;
implementation
procedure Register; begin RegisterComponents('Samples', [TnkEdit]); end;
// Component constructor constructor TnkEdit.Create(AOwner:TComponent); begin // Don't forget to call the ancestors' concstructor inherited Create(AOwner); Fmydec:=2; end;
Function Tnkedit.Getmydec:integer; begin result:=Fmydec; end;
function TnkEdit.GetNumb:single; begin try Result:=StrToFloat(text); except on EConvertError do begin Result:=0; text:='; end; end; end;
procedure Tnkedit.SetmyDEC(Value:integer); begin if value >0 then Fmydec:=Value else Fmydec:=2; end;
// Procedure to recording into FNumb procedure TnkEdit.SetNumb(value:single); begin FNumb:=Value; Text:=FloatToStr(value); end;
// Procedure for working with KeyPress event procedure TnkEdit.KeyPress(var key:char); begin case key of '0'.. '9': if (Pos('.',text)<>0)and(Fmydec=Length(text)-Pos(DecimalSeparator,text)) then key:=#0; '-': if Length(text)<>0 then key:=#0; #8, #13:; else if not ((key = DecimalSeparator) and (Pos(DecimalSeparator,text)=0)) then key:= #0; end; inherited KeyPress(key); end;
procedure Tnkedit.doExit; var i:integer; begin //输入时没有输小数部分 if pos('.',text)=0 then begin text:=text+'.'; end; // 输入时末尾是. if pos('.',text)=length(text) then begin for i:=1 to Fmydec do begin text:=text+'0'; end; end; //输入小数为数不够 if Fmydec>(length(text)-pos('.',text)) then begin for i:=length(text)-pos('.',text) to Fmydec do begin text:=text+'0'; end; end; inherited doExit; end;