⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intedit.pas

📁 实现整型编辑的控制,用得很方便 ,绝对安全
💻 PAS
字号:
unit IntEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TIntEdit = class(TCustomEdit)
  private
    FValue : LongInt;
    FMaxLength   : integer;    
    procedure SetValue( v : LongInt );
    function GetValue : LongInt;
    function StripLeadingZeros( s : string ) : string;
    Procedure SetMaxLength (Value: integer);
  protected
    procedure KeyPress( var key : char ); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property AutoSelect;
    property AutoSize;
    property BorderStyle;
    property CharCase;
    property Color;
    property Ctl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property HideSelection;
    Property MaxLength : integer Read FMaxLength Write SetMaxLength Default 10;
    property OEMConvert;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ReadOnly;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Value : LongInt read GetValue write SetValue default 0;
    property Visible;
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnStartDrag;
  end;

procedure Register;

implementation

procedure TIntEdit.KeyPress( var key : char );
var
  KeyOk : boolean;
begin
  inherited KeyPress( key );
  KeyOk :=false;
  KeyOk :=(key in ['0'..'9']) or ((key = '-') and (SelStart = 0));
  if Length(Text)>=FMaxLength then
    KeyOk:=False;
  if key = #8 then
    KeyOk :=true;
  if not KeyOk then
    Key :=#0;
end;


constructor TIntEdit.Create(AOwner: TComponent);
begin
  inherited create( AOwner );
  value :=0;
  FMaxLength:=10;
end;

procedure TIntEdit.SetValue( v : LongInt );
begin
  FValue :=v;
  Text :=IntToStr( v );
end;

function TIntEdit.GetValue : LongInt;
var
  v : LongInt;
  s : string;
begin
// error check
  if text = '' then
  begin
    GetValue :=0;
    exit;
  end;
  s :=StripLeadingZeros( text );
  v :=0;
  v :=StrToInt( s );
  GetValue :=v;
end;

function TIntEdit.StripLeadingZeros( s : string ) : string;
var
  i : integer;
  len : integer;
begin
  len :=length(s);
  for i :=1 to len do
    if s[i] <> '0' then
      break;
  // special case for a string of zeros
  if i > len then
    result :='0'
  else
    result :=copy( s, i, len-i+1 );
end;

procedure Register;
begin
  RegisterComponents('MyVcl', [TIntEdit]);
end;

Procedure TIntEdit.SetMaxLength (Value: integer);
begin
  If (Value < 20) Then
    FMaxLength := Value;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -