这个控件还是有可取之处的,但是还有一些小问题,例如如果使用鼠标右键的粘贴后退出会报错,输入方式定的太死(例如只能在开始时输入负号)。还有一些符合习惯的方式,如按下Enter键时退出焦点,获得、退出焦点时框体颜色变化等。 最近有人要我做一个类似的控件,主要是可以控制输入数字的范围,另外是否输入数字还是任意字符也可选。我把它粘上来,供大家参考,希望大家多提改进建议。 另外感谢楼上的各位。 unit Cedit;
function TCedit.checkformat(value:string):boolean; begin {check whether the text is digital format or not} if Pos('-',value)>1 then begin result:=false end else if length(value)=0 then begin result:=true; end else if Pos('-',value)=1 then begin if Pos('.',value)=2 then begin result:=false; end else begin result:=true; end; end else begin result:=true; end; end;
function TCedit.IsPasteText(value:string):boolean; var i,j,k:integer; begin result:=false; j:=0; k:=0; if text='' then begin result:=false; end else begin i:=1; while (i<=length(value))and(result=false) do begin if value[i]='.' then j:=j+1; if value[i]='-' then k:=k+1; if (not (value[i] in ['0'..'9','.','-'])) then begin result:=True; i:=i+1; end else begin i:=i+1; end; end; end; if (j>1) or (k>1) then result:=true; end;
Function TCedit.GetMinInput:double; begin result:=FMinInput; end;
Function TCedit.GetMaxInput:double; begin result:=FMaxInput; end;
procedure TCedit.SetMinInput(value:double); begin FMinInput:=value; end; procedure TCedit.SetMaxInput(value:double); begin FMaxInput:=value; end;
procedure TCedit.SetState(value:TState); begin FState:= value; end;
procedure TCedit.SetHintText(value:string); begin {while set HintText,make Text and Hint have the same value} FHintText:=value; Text:=FHintText; Hint:=Text; end;
// Procedure for working with KeyPress event procedure TCedit.KeyPress(var key:char);
begin if Text=FHintText then begin Text:=''; end; Color:=clWhite; Font.Color:=clBlack; if key=#13 then begin key:=#0; postmessage(self.Handle,WM_KEYDOWN,VK_TAB,0); end; {if select number format,you can only input digital symbal and some funtional key,'-','.'is allowed} if State=StNumber then begin if (not checkformat(text))or IsPasteText(text) then begin ShowMessage('输入格式错误,请重新输入'); text:=''; self.SetFocus; key:=#0; end else begin case key of '0'.. '9': if (Length(text)<>0) and (length(text)>pos('-',text)) then begin if ((StrToFloat(Text)-FMaxInput)>0 ) or ((StrToFloat(Text)-FMinInput)<0) then begin ShowMessage('有效输入范围'+ FloatToStr(FMinInput )+'-'+ FloatToStr(FMaxInput)+',请重新输入'); key:=#0; self.SetFocus; end; end; '-': if Pos('-',text)>0 then key:=#0; #8:; else if not ((key = DecimalSeparator) and (Pos(DecimalSeparator,text)=0)) then key:= #0; end; end; end; {one time input digital over} inherited KeyPress(key);
end; procedure TCedit.doEnter; begin Color:=clWhite; Font.Color:=clBlack; self.SelectAll; inherited doEnter; end;
procedure TCedit.doExit; begin if Text=FHintText then begin Text:=''; end; if State=StNumber then begin if text='' then text:=FloatToStr(FMinInput ); if (not checkformat(text))or IsPasteText(text) then begin ShowMessage('输入格式错误,请重新输入'); text:=''; self.SetFocus; end else begin if ((StrToFloat(Text)-FMaxInput)>0 ) or ((StrToFloat(Text)-FMinInput)<0) then begin ShowMessage('有效输入范围'+ FloatToStr(FMinInput )+'-'+ FloatToStr(FMaxInput)+',请重新输入'); self.SetFocus; end; end; end; Color:=clSilver; Font.Color:=clBlack; inherited doExit; end;
procedure TCedit.Click; begin if Text=FHintText then begin Text:=''; end; inherited Click; end;
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 (text<>'0.00')and(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;
procedure Tnkedit.SetAlignment(Value: TAlignment); begin if FAlignment <> Value then begin FAlignment := Value; RecreateWnd; end; end;
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER)); begin inherited CreateParams(Params); with Params do begin Style := Style or Alignments[UseRightToLeftAlignment, FAlignment] end; end; end.