home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / del2faq.zip / RFEDIT.ZIP / RFEDIT.PAS < prev   
Pascal/Delphi Source File  |  1995-04-01  |  3KB  |  127 lines

  1. unit RFEdit;
  2.  {Implementation of a TEdit component with
  3.    filter and required field validation  }
  4. {Copyright (c), 1995 by Wm. Romano. All Rights Reserved}
  5. interface
  6.  
  7. uses
  8.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  9.   Forms, Dialogs, StdCtrls;
  10.  
  11. type
  12.   TValidChar=String;
  13.   TRFilterEdit = class(TEdit)
  14.   private
  15.     { Private declarations }
  16.   protected
  17.   FRequired : Boolean;
  18.   FValidch  : TValidChar;
  19.   ValidChar : set of char;
  20.     { Protected declarations }
  21.   Procedure SetValidCh(Value:TValidChar);
  22.   Procedure SetRequired(Value:Boolean);
  23.   public
  24.     { Public declarations }
  25.   Constructor Create(AOwner:TComponent); override;
  26.   procedure KeyPress(var Key: Char); override;
  27.   Procedure DoExit; override;
  28.   published
  29.     { Published declarations }
  30.   Property ValidChars:TValidChar
  31.                   read FValidCh
  32.                     Write SetValidCh;
  33.   Property RequiredField:Boolean read FRequired
  34.                             write SetRequired;
  35.   end;
  36.  
  37. procedure Register;
  38.  
  39. implementation
  40. Constructor TRFilterEdit.Create(AOwner:TComponent);
  41. begin
  42.   inherited Create(AOwner);
  43.   FValidCh:='[]';
  44.   FRequired:=False;
  45. end;
  46. Procedure TRFilterEdit.SetRequired(Value:Boolean);
  47. Begin
  48.   if Value<>FRequired then
  49.     FRequired:=Value;
  50. end;
  51.  
  52. procedure TRFilterEdit.DoExit;
  53. begin
  54.   inherited DoExit;
  55.   if FRequired then
  56.    begin
  57.      if Text = '' then
  58.      Begin
  59.        MessageDlg(' Field is required! ',mtWarning, [mbOK],0);
  60.        SetFocus;
  61.      end;
  62.    end;
  63. end ;
  64.  
  65.  
  66.  
  67.  
  68. Procedure TRFilterEdit.SetValidCh(Value:TValidChar);
  69.   var
  70.   vc3 :  set of char;
  71.   x   :  integer;
  72.   ch1,ch2,ch3 : char;
  73.   SetRange    :  boolean;
  74.  
  75.   Procedure MakeSet;
  76.   Begin
  77.     if SetRange then vc3:=[ch1..ch2]
  78.     else
  79.     vc3:=[ch1];
  80.     validchar:=validchar+vc3;
  81.     ch1:=#0;ch2:=#0;ch3:=#0;
  82.     SetRange:=False;
  83.   End;
  84.  
  85. Begin {SetValidCh}
  86.   SetRange:=False;
  87.   if Value<>FValidCh then
  88.    BEGIN
  89.      FValidCh:=Value;
  90.      if (FValidCh[1]='[') and
  91.        (FValidCh[length(FValidCh)]=']' )then
  92.      For x:=2 to length(FValidCh) do
  93.      begin
  94.        ch3:=FValidCh[x];
  95.        if (ch3=',') or (ch3=']') then
  96.        begin
  97.          MakeSet;
  98.        end;
  99.        if ch3='.' then SetRange:=True;
  100.        if ch3='''' then
  101.        begin
  102.          ch3:=FValidCh[x+1];
  103.          x:=x+2;
  104.        end;
  105.        if (SetRange=False) then ch1:=ch3
  106.        else ch2:=ch3;
  107.     end
  108.     else
  109.     FValidCh:='Invalid Format '+ FValidCh;
  110.   end;
  111. end;
  112.  
  113.  procedure TRFilterEdit.KeyPress(Var Key: Char);
  114.  begin
  115.    if ValidChar <>[] then
  116.     if (NOT (Key in ValidChar))
  117.         and (Key<>#08) then  Key:=#0;
  118.    inherited KeyPress(Key);
  119.  end;
  120.  
  121. procedure Register;
  122. begin
  123.   RegisterComponents('Additional', [TRFilterEdit]);
  124. end;
  125.  
  126. end.
  127.