Thursday, December 21, 2006


IntEdit

Aquest component és una caixa d'edicíó que només permet l'entrada de nombre sencers.

unit IntEdit;

interface

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

type TIntEditEd = class(TDefaultEditor)
  procedure ExecuteVerb(Index: integer); override;
  function GetVerb(Index: integer): string; override;
  function GetVerbCount: integer; override;
  end;

type
  TIntEdit = class(TCustomEdit)
  private
    { Private declarations }
    FAlignment: TAlignment;
    FValue: LongInt;
    FMaxValue: LongInt;
    lDelPressed: boolean;
    procedure SetAlignment(AAlignment: TAlignment);
    function GetFieldValue: LongInt;
    procedure SetFieldValue(iNewVal: LongInt);
    function GetAsInteger: LongInt;
    procedure SetAsInteger(lnVal: LongInt);
    function GetAsString: string;
    procedure SetAsString(strVal: string);
    procedure SetMaxValue(Value: LongInt);
    procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
  protected
    { Protected declarations }
    procedure CreateParams(var Params: TCreateParams); override;
    procedure KeyPress(var Key: Char); override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    property AsInteger: LongInt read GetAsInteger write SetAsInteger;
    property AsString: string read GetAsString write SetAsString;
  published
    { Published declarations }
    property Value: LongInt read GetFieldValue write SetFieldValue;
    property MaxValue: LongInt read FMaxValue write SetMaxValue;
    property Alignment: TAlignment read FAlignment write SetAlignment default taRightJustify;
    property Anchors;
    property AutoSelect;
    property AutoSize default True;
    property BorderStyle;
    property Color;
    property Ctl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property HideSelection;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ReadOnly;
    property ShowHint;
    property TabOrder;
    property Visible;
    (* General events properties *)
    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;
  end;
type
  TIntEdit = class(TIntEdit);

procedure Register;

implementation

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

constructor TIntEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FAlignment := taRightJustify;
  FValue := 0;
  Text := IntToStr(FValue);
  FMaxValue := 2147483647;
  lDelPressed := False;
end;

procedure TIntEdit.SetAlignment(AAlignment: TAlignment);
begin
  if FAlignment <> AAlignment then
  begin
    FAlignment := AAlignment;
    if not (csDesigning in ComponentState) then
      RecreateWnd;
    if Focused then
      SelectAll;
  end;
end;

function TIntEdit.GetFieldValue: LongInt;
begin
  Result := FValue;
end;

procedure TIntEdit.SetFieldValue(iNewVal: LongInt);
begin
    if iNewVal<=FMaxValue then
    begin
      Text := IntToStr(iNewVal);
      FValue := iNewVal;
    end;
    if Focused then
    begin
      SelStart := Length(Text);
      SelectAll;
    end;
end;

procedure TIntEdit.SetMaxValue(Value: LongInt);
begin
  FMaxValue := Value;
end;

procedure TIntEdit.SetAsInteger(lnVal: LongInt);
begin
  Value := lnVal;
end;

function TIntEdit.GetAsInteger: LongInt;
begin
  Result := Value;
end;

procedure TIntEdit.SetAsString(strVal: string);
begin
  Value := StrToInt(strVal);
end;

function TIntEdit.GetAsString: string;
begin
  Result := IntToStr(Value);
end;

procedure TIntEdit.KeyPress(var Key: Char);
begin
    if not (Key in [#27, '0'..'9', #8, #13]) then
    begin
      Key := #0;
      Exit;
    end;
    try
      if not (Key in [#27, #8, #13]) then
        FValue := StrToInt(Text+Key);
      inherited;
    except
      Key := #0;
    end;
  inherited KeyPress(Key);
end;

procedure TIntEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
  ClearSelection;
  inherited KeyDown(Key,Shift);
end;

procedure TIntEdit.KeyUp(var Key: Word; Shift: TShiftState);
begin
  inherited KeyUp(Key,Shift);
  if Text='' then
  begin
    Text := '0';
    SelStart := Length(Text);
    SelectAll;
  end;
  FValue := StrToInt(Text);
end;


procedure TIntEdit.CreateParams(var Params: TCreateParams);
var
{$ifdef VER120}
  lnStyle: LongWord;
{$else}
  lnStyle: LongInt;
{$endif}
begin
  inherited CreateParams(Params);
  case Alignment of
    taLeftJustify:
      lnStyle := ES_LEFT;
    taRightJustify:
      lnStyle := ES_RIGHT
    else
      lnStyle := ES_CENTER;
  end;
  with Params do
    Style := Style or DWORD(lnStyle);
end;

procedure TIntEdit.CMEnter(var Message: TCMEnter);
begin
  if AutoSelect and not ReadOnly then
    SelectAll
  else
    if not ReadOnly then
    begin
      SelStart := Length(Text);
    end;
  Refresh;
  inherited;
end;

end.

El nombre introduït s'obté en la propietat Value. La propietat MaxValue ens permet limitar el màxim nombre a introduir. Tenim també la propietat Alignment, per alinear el text a la dreta de la caixa d'edició.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?