📄 numedit.pas
字号:
unit NumEdit;
interface
uses
SysUtils, Classes, Controls, StdCtrls;
type
TNumEdit = class(TEdit)
private
{ Private declarations }
pPositive:boolean;
pDeciNum:integer;
FOnCustKeyPress: TKeyPressEvent;
function getvalue: Double;
procedure setvalue(Value: Double);
function GetIntValue: integer;
procedure SetIntValue(value: integer);
protected
{ Protected declarations }
procedure KeyPress(var Key: Char); override;
public
{ Public declarations }
constructor create(aowner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Positive:boolean read pPositive write pPositive;
property DeciNum:integer read pDeciNum write pDeciNum;
property Value:double read getvalue write setvalue;
property IntValue:integer read getIntvalue write setIntvalue;
property OnCustKeyPress: TKeyPressEvent read FOnCustKeyPress write FOnCustKeyPress;
end;
procedure Register;
implementation
uses Math;
procedure Register;
begin
RegisterComponents('Standard', [TNumEdit]);
end;
{ TNumEdit }
constructor TNumEdit.create(aowner: TComponent);
begin
inherited;
ControlStyle := ControlStyle - [csSetCaption];
pPositive:=true;
pDeciNum:=4;
end;
destructor TNumEdit.Destroy;
begin
inherited;
end;
function TNumEdit.GetIntValue: integer;
begin
if Text='' then
begin
Result:=0;
exit;
end;
try
Result:=StrToInt(Text);
Except
Result:=0;
end;
end;
function TNumEdit.getvalue: Double;
begin
if Text='' then
begin
Result:=0;
exit;
end;
try
Result:=StrToFloat(Text);
Except
Result:=0;
end;
end;
procedure TNumEdit.KeyPress(var Key: Char);
begin
if ord(key)=8 then
begin
inherited;
exit;
end;
if Key in ['0'..'9','.','-'] then
begin
if key='-' then
begin
if pPositive=true then
begin
key:=chr(0);
exit;
end;
if SelStart<>0 then
begin
key:=chr(0);
exit;
end;
if pos('-',Text)>0 then
begin
key:=chr(0);
exit;
end;
end;
if key='.' then
begin
if pos('.',Text)>0 then
begin
key:=chr(0);
exit;
end;
if SelStart=0 then
begin
key:=chr(0);
exit;
end;
if pos('-',Text)=1 then
begin
if SelStart=1 then
begin
key:=chr(0);
exit;
end;
end;
end;
if key in ['0'..'9'] then
begin
if pos('-',Text)>0 then
begin
if SelStart=0 then
begin
key:=chr(0);
exit;
end;
end;
if pos('0',Text)=1 then
begin
if SelStart=1 then
begin
key:=chr(0);
exit;
end;
if (key='0') and (selStart=0) then
begin
key:=chr(0);
exit;
end;
end;
if (pos('-',Text)=1) and (pos('0',Text)=2) then
begin
if (selstart=2) then
begin
key:=chr(0);
exit;
end;
if (key='0') and (SelStart=1) then
begin
key:=chr(0);
exit;
end;
end;
if pos('.',Text)>0 then
begin
if (Length(Text)-pos('.',Text))>=DeciNum then
begin
if SelStart>=Pos('.',Text) then
begin
key:=chr(0);
exit;
end;
end;
end;
end;
inherited;
exit;
end;
if Assigned(FOnCustKeyPress) then
FOnCustKeyPress(self,Key);
key:=chr(0);
exit;
end;
procedure TNumEdit.SetIntValue(value: integer);
begin
Text:=IntToStr(Value);
end;
procedure TNumEdit.setvalue(Value: Double);
begin
Text:=FloatToStr(Value);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -