Lazarus

Programming => Packages and Libraries => Lazarus Extra Components => Topic started by: JorgeGoias on September 25, 2008, 08:19:49 pm

Title: Whats wrong with new TIntEdit component
Post by: JorgeGoias on September 25, 2008, 08:19:49 pm
Whats wrong with this new component?

In use when press one simple key, will trigger OnExit of descendants!

Please, anybody helping?

----------------------------------------------------------------------------------
unit intedit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Controls, Graphics, StdCtrls, LCLType, LResources;

type
  TIntEdit = class(TCustomMemo)
  private
    FValue: LongWord;
    procedure SetFieldValue(A: LongWord);
    procedure FormatText;
  protected
    procedure DoEnter; override;
    procedure EditingDone;; override;
    procedure KeyPress(var Key: Char); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Value: Longword read FValue write SetFieldValue;

    property Alignment default taRightJustify;
    property BorderStyle;
    property Color;
    property Ctl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property MaxLength;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ReadOnly;
    property ShowHint;
    property TabOrder;
    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;
  end;

implementation

function trim_value(const str: string): string;
var
   i:   longword;
   tam: longword;
   ret: string;
begin
   ret := '';
   tam := Length(str);
   for i := 1 to tam do
   begin
      if (str in ['0'..'9']) then
      begin
         ret += str;
      end;
   end;

   result := ret;
end;

constructor TIntEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width       := 121;
  Height      := 21;
  Alignment   := taRightJustify;
  AutoSize    := False;
  AutoSelect  := False;
  WantReturns := False;
  WordWrap    := False;

  FValue      := 0;
  Text        := '0';
  FormatText;
end;

procedure TIntEdit.SetFieldValue(A: Longword);
begin
  if (FValue <> A) then
    begin
      FValue := A;
      FormatText;
    end;
end;

procedure TIntEdit.FormatText;
begin
  Text := IntToStr(FValue);
end;

procedure TIntEdit.DoEnter;
begin
  SelectAll;
  inherited;
end;

procedure TIntEdit.EditingDone;
begin
  inherited EditingDone;
 
  FValue := 0;
  Text   := Trim(Text);
  if (Text <> '') then
  begin
     FValue := StrToInt(trim_value(Text));
     FormatText;
  end;
end;

procedure TIntEdit.KeyPress(var Key: Char);
begin
  if Not (Key in ['0'..'9', #8, #13]) then
  begin
     Key := #0;
     Exit;
  end;
 
  inherited KeyPress(Key);
end;

end.

-----------------------------------------------------------------------------------
TinyPortal © 2005-2018