home *** CD-ROM | disk | FTP | other *** search
/ Freelog 11 / Freelog011.iso / BestOf / PhoenixMail / Source / comps / NumEdit.pas < prev    next >
Pascal/Delphi Source File  |  1999-02-10  |  3KB  |  127 lines

  1. {*****************************************************************************
  2.  *
  3.  *  NumEdit.pas - TNumEdit Component
  4.  *
  5.  *  Copyright (c) 1998-99 Michael Haller
  6.  *
  7.  *  Author:     Michael Haller
  8.  *  E-mail:     michael@discountdrive.com
  9.  *  Homepage:   http://www.discountdrive.com/sunrise/
  10.  *
  11.  *  This program is free software; you can redistribute it and/or
  12.  *  modify it under the terms of the GNU General Public License
  13.  *  as published by the Free Software Foundation;
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  *
  24.  *----------------------------------------------------------------------------
  25.  *
  26.  *  Revision history:
  27.  *
  28.  *     DATE     REV                 DESCRIPTION
  29.  *  ----------- --- ----------------------------------------------------------
  30.  *
  31.  *****************************************************************************}
  32.  
  33. unit NumEdit;
  34.  
  35. interface
  36.  
  37. uses
  38.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  39.   StdCtrls;
  40.  
  41. type
  42.   TNumEdit = class(TEdit)
  43.   private
  44.     FMin: Integer;
  45.     FMax: Integer;
  46.     FValue: Integer;
  47.     procedure MakeValidText;
  48.     function GetValue: Integer;
  49.     procedure SetValue(I: Integer);
  50.   protected
  51.     procedure KeyPress(var Key: Char); override;
  52.     procedure WndProc(var Message: TMessage); override;
  53.   public
  54.     constructor Create(AOwner: TComponent); override;
  55.   published
  56.     property Min: Integer read FMin write FMin;
  57.     property Max: Integer read FMax write FMax;
  58.     property Value: Integer read GetValue write SetValue;
  59.   end;
  60.  
  61. procedure Register;
  62.  
  63. implementation
  64.  
  65. procedure TNumEdit.MakeValidText;
  66. var
  67.   I: LongInt;
  68. begin
  69.   I := FMin;
  70.   try
  71.     if Text = '' then I := 0 else I := StrToInt(Text);
  72.     if I > FMax then I := FMax;
  73.     if I < FMin then I := FMin;
  74.   finally
  75.     Text := IntToStr(I);
  76.   end;
  77. end;
  78.  
  79. function TNumEdit.GetValue: Integer;
  80. begin
  81.   Result := 0;
  82.   if (Text <> '') and (Text <> '-') then begin
  83.     try
  84.       Result := StrToInt(Text);
  85.     except end;
  86.   end;
  87.   FValue := Result;
  88. end;
  89.  
  90. procedure TNumEdit.SetValue(I: Integer);
  91. begin
  92.   try
  93.     Text := IntToStr(I);
  94.   except end;
  95.   FValue := StrToInt(Text);
  96. end;
  97.  
  98. procedure TNumEdit.KeyPress(var Key: Char);
  99. begin
  100.   inherited KeyPress(Key);
  101.   if Key in ['0'..'9', #8, '-'] = False then Key := Chr(0);
  102. end;
  103.  
  104. procedure TNumEdit.WndProc(var Message: TMessage);
  105. begin
  106.   inherited WndProc(Message);
  107.   if Message.Msg = WM_KILLFOCUS then MakeValidText;
  108. end;
  109.  
  110. constructor TNumEdit.Create(AOwner: TComponent);
  111. begin
  112.   inherited Create(AOwner);
  113.   FMin := 0;
  114.   FMax := 100;
  115.   AutoSize := False;
  116.   Height := 17;
  117.   MaxLength := 5;
  118.   Text := '0';
  119. end;
  120.  
  121. procedure Register;
  122. begin
  123.   RegisterComponents('Michael Haller', [TNumEdit]);
  124. end;
  125.  
  126. end.
  127.