home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 March / Chip_1999-03_cd.bin / zkuste / delphi / D / EDITNEW.ZIP / EditNew.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-12-05  |  24.1 KB  |  783 lines

  1. {TEditN, TMEditN, TDBEditN
  2.  
  3.  - Author   : Jose Maria Gias
  4.  - email    : sigecom@arrakis.es
  5.  - Version  : 2.2 Delphi 2-3-4
  6.  - Date     : 03.12.98
  7.  - Type     : FreeWare
  8.  
  9.  Comments in file ReadENew.Txt
  10.  }
  11. unit EditNew;
  12.  
  13. interface
  14.  
  15. uses
  16.   {$IFDEF WIN32}Windows,{$ELSE}Winprocs,{$ENDIF}
  17.   Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  18.   StdCtrls, Mask, DBCtrls;
  19.  
  20. type
  21.   TEditTypes = (etString, etInteger, etFloat,etDate,etTime);
  22.   TEditAlign = (etAlignRight, etAlignLeft, etAlignCenter, etAlignNone, etAlignValue);
  23.  
  24.   TEditN = class(TEdit)
  25.   private
  26.     { Private declarations }
  27.     FOnEnter      : TNotifyEvent;
  28.     FOnExit       : TNotifyEvent;
  29.     FOnChange     : TNotifyEvent;
  30.     I_Color       : TColor;
  31.     E_Color       : TColor;
  32.     FI_Color      : TColor;
  33.     FE_Color      : TColor;
  34.     TipoEdit      : TEditTypes;
  35.     TipoAlign     : TEditAlign;
  36.     KeyTab        : Char;
  37.     LongAlign     : Integer;
  38.     ValInteger    : Integer;
  39.     ValFloat      : Double;
  40.     SDecimal      : Char;
  41.     EPrecision    : Integer;
  42.     FUpper        : Boolean;
  43.     FUpperList    : String;
  44.     ValTemp       : Extended;
  45.     TxtConvert    : String;
  46.     FWidthOnFocus : Integer;
  47.     iWidth        : Integer;
  48.     TextAtEnter   : String;
  49.     PtrToData     : Pointer;
  50.     sDate         : Char;
  51.     sTime         : Char;
  52.     FSeconds      : Boolean;
  53.     ValDate       : TDateTime;
  54.     ValTime       : TDateTime;
  55.   protected
  56.     {Protected declarations}
  57.     procedure FormatDate;
  58.     procedure FormatTime;
  59.   public
  60.     procedure KeyPress(var Key: Char); override;
  61.     procedure DoEnter; override;
  62.     procedure DoExit; override;
  63.     procedure Change; override;
  64.     procedure SetInteger(VInteger : Integer);
  65.     procedure SetFloat(VFloat : Double);
  66.     procedure SetPtrToData(DataPtr:Pointer);
  67.     procedure Update; // Not declare override because make stack overflow
  68.     constructor Create(AOwner : TComponent); override;
  69.   published
  70.     property  OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
  71.     property  OnExit : TNotifyEvent read FOnExit  write FOnExit;
  72.     property  OnChange : TNotifyEvent read FOnChange  write FOnChange;
  73.     property  ColorOnFocus : TColor read I_Color write I_Color;
  74.     property  ColorOnNotFocus : TColor read E_Color write E_Color;
  75.     property  FontColorOnFocus : TColor read FI_Color write FI_Color;
  76.     property  FontColorOnNotFocus : TColor read FE_Color write FE_Color;
  77.     property  EditType : TEditTypes read TipoEdit write TipoEdit;
  78.     property  EditKeyByTab : Char read KeyTab write KeyTab;
  79.     property  EditAlign : TEditAlign read TipoAlign write TipoAlign;
  80.     property  EditLengthAlign : Integer read LongAlign write LongAlign;
  81.     property  EditPrecision : Integer read EPrecision write EPrecision;
  82.     property  ValueFloat : Double read ValFloat write ValFloat;
  83.     property  ValueInteger : Integer read ValInteger write ValInteger;
  84.     property  ValueDate : TDateTime read ValDate write ValDate;
  85.     property  ValueTime : TDateTime read ValTime write ValTime;
  86.     property  TimeSeconds : Boolean read FSeconds write FSeconds;
  87.     property  FirstCharUpper : Boolean read FUpper write FUpper;
  88.     property  FirstCharUpList : String read FUpperList write FUpperList;
  89.     property  WidthOnFocus : Integer read FWidthOnFocus write FWidthOnFocus;
  90.   end;
  91.  
  92. type
  93.   TMEditN = class(TMaskEdit)
  94.   private
  95.     { Private declarations }
  96.     FOnEnter      : TNotifyEvent;
  97.     FOnExit       : TNotifyEvent;
  98.     I_Color       : TColor;
  99.     E_Color       : TColor;
  100.     FI_Color      : TColor;
  101.     FE_Color      : TColor;
  102.     FKeyTab       : Char;
  103.     FWidthOnFocus : Integer;
  104.     iWidth        : Integer;
  105.   protected
  106.     { Protected declarations }
  107.   public
  108.     { Public declarations }
  109.     procedure KeyPress(var Key: Char); override;
  110.     procedure DOEnter; override;
  111.     procedure DOExit ; override;
  112.     constructor Create(AOwner : TComponent); override;
  113.   published
  114.     { Published declarations }
  115.     property  OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
  116.     property  OnExit : TNotifyEvent read FOnExit  write FOnExit;
  117.     property  ColorOnFocus : TColor read I_Color write I_Color;
  118.     property  ColorOnNotFocus : TColor read E_Color write E_Color;
  119.     property  FontColorOnFocus : TColor read FI_Color write FI_Color;
  120.     property  FontColorOnNotFocus : TColor read FE_Color write FE_Color;
  121.     property  EditKeyByTab : Char read FKeyTab write FKeyTab;
  122.     property  WidthOnFocus : Integer read FWidthOnFocus write FWidthOnFocus;
  123.   end;
  124.  
  125. type
  126.   TDBEditN = class(TDBEdit)
  127.   private
  128.     { Private declarations }
  129.     FOnEnter      : TNotifyEvent;
  130.     FOnExit       : TNotifyEvent;
  131.     I_Color       : TColor;
  132.     E_Color       : TColor;
  133.     FI_Color      : TColor;
  134.     FE_Color      : TColor;
  135.     FKeyTab       : Char;
  136.     FWidthOnFocus : Integer;
  137.     iWidth        : Integer;
  138.     FUpper        : Boolean;
  139.     FUpperList    : String;
  140.   protected
  141.     { Protected declarations }
  142.   public
  143.     { Public declarations }
  144.     procedure KeyPress(var Key: Char); override;
  145.     procedure DOEnter; override;
  146.     procedure DOExit ; override;
  147.     constructor Create(AOwner : TComponent); override;
  148.   published
  149.     { Published declarations }
  150.     property  OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
  151.     property  OnExit : TNotifyEvent read FOnExit  write FOnExit;
  152.     property  ColorOnFocus : TColor read I_Color write I_Color;
  153.     property  ColorOnNotFocus : TColor read E_Color write E_Color;
  154.     property  FontColorOnFocus : TColor read FI_Color write FI_Color;
  155.     property  FontColorOnNotFocus : TColor read FE_Color write FE_Color;
  156.     property  EditKeyByTab : Char read FKeyTab write FKeyTab;
  157.     property  FirstCharUpper : Boolean read FUpper write FUpper;
  158.     property  FirstCharUpList : String read FUpperList write FUpperList;
  159.     property  WidthOnFocus : Integer read FWidthOnFocus write FWidthOnFocus;
  160.   end;
  161.  
  162. procedure Register;
  163.  
  164. implementation
  165.  
  166. {$R EdNew32.res}
  167.  
  168. constructor TEditN.Create(AOwner : TComponent);
  169. begin
  170.   inherited Create(AOwner);
  171.   ColorOnFocus        := clWhite;
  172.   ColorOnNotFocus     := clSilver;
  173.   Color               := ColorOnNotFocus;
  174.   FontColorOnFocus    := clRed;
  175.   FontColorOnNotFocus := clBlack;
  176.   TipoEdit            := etString;
  177.   TipoAlign           := etAlignNone;
  178.   LongAlign           := 0;
  179.   KeyTab              := #9;        // #13 for Return by Tab
  180.   ValInteger          := 0;
  181.   ValFloat            := 0;
  182.   EPrecision          := 0;
  183.   SDecimal            := DecimalSeparator;
  184.   FUpper              := False;
  185.   FUpperList          := ' (';
  186.   FWidthOnFocus       := 0;
  187.   TextAtEnter         := '';
  188.   PtrToData           := nil;
  189.   sDate               := DateSeparator;   // Windows Default
  190.   sTime               := TimeSeparator;   // Windows Default
  191.   FSeconds            := False;           // etTime with seconds
  192.   ValDate             := Date;
  193.   ValTime             := Time;
  194. end;
  195.  
  196. procedure TEditN.SetPtrToData(DataPtr:Pointer);
  197. begin
  198.  PtrToData := DataPtr;
  199.  Update;
  200. end;
  201.  
  202. procedure TEditN.Update;
  203. begin
  204.  if Assigned(PtrToData) then begin
  205.   if EditType = etString  then Text := string(PtrToData^);
  206.   if EditType = etInteger then Text := IntToStr(Integer(PtrToData^));
  207.   if EditType = etFloat   then Text := FloatToStrF(Double(PtrToData^),ffgeneral,15,4);
  208.   if EditType = etDate    then Text := DateToStr(TDateTime(PtrToData^));
  209.   if EditType = etTime    then Text := TimeToStr(TDateTime(PtrToData^));
  210.  end;
  211.  Refresh;
  212.  inherited Update;
  213. end;
  214.  
  215. procedure TEditN.KeyPress(var Key: Char);
  216. var
  217.  {$IFDEF VER80}
  218.   FEditTemp : TForm;       {For Delphi 1}
  219.  {$ENDIF}
  220.  
  221.  {$IFDEF VER90}
  222.   FEditTemp : TForm;       {For Delphi 2}
  223.  {$ENDIF}
  224.  
  225.  {$IFDEF VER100}
  226.   FEditTemp : TCustomForm; {For Delphi 3}
  227.  {$ENDIF}
  228.  
  229.  {$IFDEF VER120}
  230.   FEditTemp : TCustomForm; {For Delphi 4}
  231.  {$ENDIF}
  232.  
  233.  C         : String;
  234. begin
  235.  
  236.  if Key = EditKeyByTab then begin
  237.   FEditTemp := GetParentForm(Self);
  238.   SendMessage(FEditTemp.Handle, WM_NEXTDLGCTL, 0, 0);
  239.   Key := #0;
  240.  end else begin
  241.  
  242.   // If ESC is pressed during edit, all changes are cancelled
  243.   // Si se ha pulsado escape, se anulan los cambios
  244.   if Key = #27 then begin
  245.    Text := TextAtEnter;
  246.    Key  := #15;
  247.   end;
  248.  
  249.   //Permitted characters in function of type
  250.   // Caracteres permitidos en funci≤n del tipo
  251.   Case EditType of
  252.    etString :
  253.     if FUpper then begin // Capital letter  - Ma²usculas
  254.      if (Length(Text) = 0) or
  255.         (SelText = Text) or
  256.         (Pos(Text[Length(Text)],FUpperList) > 0) then begin
  257.       C   := AnsiUpperCase(Key);
  258.       Key := C[1];
  259.      end;
  260.     end;
  261.  
  262.    etInteger :
  263.     begin
  264.      if ((Pos('-',Text) > 0) or (Key = '-')) and (MaxLength = 0)
  265.       then MaxLength := 11;
  266.  
  267.      if (not (Key in ['0'..'9','-',#8,#13,#35,#36,#37,#39])) or
  268.         (Key = #32) or // To eliminate the introduction from spaces
  269.         ((Key = '-') and (Pos('-',Text) > 0)) // To verify that alone is introduce a negative sign.
  270.       then Key := #15;
  271.  
  272.     end;
  273.  
  274.    etFloat :
  275.     begin
  276.      if (not (Key in ['0'..'9',',','.','-',#8,#13,#35,#36,#37,#39])) or
  277.         (Key = #32) or // To eliminate the spaces introduction
  278.         ((Key = '-') and (Pos('-',Text) > 0)) // To verify that alone is introduce a negative sign.
  279.       then Key := #15;
  280.  
  281.      if (Key = ',') or (Key = '.') then
  282.       if (Pos(',',Text) > 0) or (Pos('.',Text) > 0)
  283.        then Key := #15
  284.        else Key := DecimalSeparator;
  285.  
  286.     end;
  287.  
  288.    etDate, etTime :
  289.     if not (Key in ['0'..'9',#8,#13,#35,#36,#37,#39])
  290.      then Key := #15;
  291.  
  292.   end; // Case EditType of
  293.  end;  // if Key <> EditKeyByTab
  294.  
  295.  if Key <> #0 then inherited KeyPress(Key);
  296.  
  297. end;
  298.  
  299. procedure TEditN.DoEnter;
  300. begin
  301.  // To assign the Color upon receiving the focus
  302.  if (EditType = etFloat) and (MaxLength = 0) then MaxLength := 16;
  303.  Color       := ColorOnFocus;
  304.  Font.Color  := FontColorOnFocus;
  305.  TextAtEnter := Text;
  306.  
  307.  if WidthOnFocus > 0 then begin
  308.   iWidth := Width;
  309.   Width  := FWidthOnFocus;
  310.  end;
  311.  
  312.  // If a connection to a variable exists, Update the contents of the field with
  313.  // the contents of the connected variable in case the variable has changed.
  314.  if Assigned(PtrToData) then Update;
  315.  
  316.  if EditType = etDate then MaxLength := 10;
  317.  
  318.  if EditType = etTime then
  319.   if TimeSeconds then MaxLength := 8
  320.                  else MaxLength := 5;
  321.  
  322.  if Assigned(FOnEnter) then FOnEnter(Self);
  323. end;
  324.  
  325. procedure TEditN.DoExit;
  326. var
  327.  k : Integer;
  328.  s : String;
  329. begin
  330.  
  331.  // To return the color of the fund upon leaving and losing the focus
  332.  Color      := ColorOnNotFocus;
  333.  Font.Color := FontColorOnNotFocus;
  334.  
  335.  if WidthOnFocus > 0 then Width := iWidth;
  336.  
  337.  if (EditType = etString) and (Length(Text) > 0) then begin
  338.  
  339.   if FUpper then begin
  340.    if Length(Text) = 1 then Text := AnsiUpperCase(Text);
  341.    if Length(Text) > 1 then Text := AnsiUpperCase(Text[1]) + Copy(Text,2,Length(Text)-1);
  342.   end;
  343.  
  344.   if (EditAlign <> etAlignNone) and (EditLengthAlign > 0) then begin // With Alignment
  345.  
  346.    // The length of the chain is < that that of Align.
  347.    if (EditLengthAlign > Length(Text)) then
  348.     Case EditAlign of
  349.      etAlignLeft  :
  350.       begin
  351.        while Text[1] = ' ' do Text := Copy(Text,2,Length(Text)-1);
  352.        for k := 1 to EditLengthAlign - Length(Text) do Text := Text + ' ';
  353.       end;
  354.  
  355.      etAlignRight :
  356.       begin
  357.        for k:= 1 to EditLengthAlign - Length(Text) do Text := ' ' + Text;
  358.       end;
  359.  
  360.      etAlignCenter:
  361.       begin
  362.        for k := 1 to Round((EditLengthAlign - Length(Text))/2) do Text := ' ' + Text;
  363.        for k := Length(Text) to EditLengthAlign do Text := Text + ' ';
  364.       end;
  365.  
  366.     end; // Case EditAlign
  367.   end; // if (EditLengthAlign > Length(Text))
  368.  end; // if (EditAlign <> etAlignNone) and (EditLengthAlign > 0)
  369.  
  370.  // To align a string Integer, filling with zeroes, if it has been indicated.
  371.  // The negative sign if exists, counts it as a digit but
  372.  if (EditType = etInteger) and
  373.     (EditAlign = etAlignValue) and
  374.     (EditLengthAlign > 0) then
  375.   if Length(Text) < EditLengthAlign then
  376.    for k := Length(Text) to EditLengthAlign - 1 do Text := '0' + Text;
  377.  
  378.  // To put the negative sign to the beginning of the chain. It has been designed
  379.  // so that the negative sign could be introduced in any place, and here we happen
  380.  // it to the beginning
  381.  if ((EditType = etInteger) or (EditType = etFloat)) and (Pos('-',Text) > 1) then
  382.   if Length(Text) = Pos('-',Text)
  383.    then Text := '-' + Copy(Text,1,Pos('-',Text)-1)
  384.    else Text := '-' +
  385.                 Copy(Text,1,Pos('-',Text)-1) +
  386.                 Copy(Text,Pos('-',Text) + 1,Length(Text) - Pos('-',Text));
  387.  
  388.  // If it has been defined precision, gives format  to the string
  389.  if (EditType = etFloat) and (EditPrecision > 0) then begin
  390.   if Length(Text) = 0 then Text := '0';
  391.   SDecimal := DecimalSeparator;
  392.   if Pos(SDecimal,Text) = 0 then begin
  393.    Text := Text + SDecimal;
  394.    for k := 1 to EditPrecision do Text := Text + '0';
  395.   end else begin
  396.    if Length(Text) - Pos(SDecimal,Text) > EditPrecision then
  397.     Text := Copy(Text,1,Pos(SDecimal,Text) + EditPrecision);
  398.    if Length(Text) - Pos(SDecimal,Text) < EditPrecision then
  399.     for k := Length(Text) - Pos(SDecimal,Text) + 1 to EditPrecision do Text := Text + '0';
  400.   end;
  401.  end;
  402.  
  403.  // To align a string Float, filling of zeroes, if it has been indicated.
  404.  // The negative sign if exists and the separating decimal, the account as a digit but
  405.  if (EditType = etFloat) and
  406.     (EditAlign = etAlignValue) and
  407.     (EditLengthAlign > 0) then
  408.   if Length(Text) < EditLengthAlign then
  409.    for k := Length(Text) to EditLengthAlign - 1 do Text := '0' + Text;
  410.  
  411.  if EditType = etDate then FormatDate;
  412.  
  413.  if EditType = etTime then FormatTime;
  414.  
  415.  // Update the connected variable with the current value
  416.  if Assigned(PtrToData) then begin
  417.   if EditType = etInteger then Move(ValueInteger, PtrToData^, Sizeof(ValueInteger));
  418.   if EditType = etFloat   then Move(ValueFloat,   PtrToData^, Sizeof(ValueFloat));
  419.   if EditType = etDate    then Move(ValueDate,    PtrToData^, Sizeof(ValueDate));
  420.   if EditType = etTime    then Move(ValueTime,    PtrToData^, Sizeof(ValueTime));
  421.   if EditType = etString  then begin
  422.    s := Text;
  423.    Move(s, PtrToData^, Sizeof(s));
  424.   end;
  425.  end;
  426.  
  427.  if Assigned(FOnExit) then FOnExit(Self);
  428. end;
  429.  
  430. procedure TEditN.Change;
  431. var
  432.  i : Integer;
  433.  C : String;
  434. begin
  435.  // To convert the chain if it is numerical,to return a value
  436.  if ((EditType = etInteger) or (EditType = etFloat)) and
  437.     (Length(Text) > 0) then begin
  438.  
  439.   if EditType = etInteger then begin
  440.    for i := 1 to Length(Text) do begin
  441.     if Text[i] in ['0'..'9','-','+'] then C := C + Text[i]
  442.    end;
  443.    Text := C;
  444.   end;
  445.  
  446.   if EditType = etFloat then begin
  447.    for i := 1 to Length(Text) do begin
  448.     if Text[i] in ['0'..'9',',','.','-','+'] then C := C + Text[i]
  449.    end;
  450.    Text := C;
  451.   end;
  452.  
  453.   if Length(Text) = 0 then begin
  454.    if Assigned(FOnChange) then FOnChange(Self);
  455.    Exit;
  456.   end;
  457.  
  458.   try
  459.    ValueFloat   := 0;
  460.    ValueInteger := 0;
  461.  
  462.    // Eliminar caracteres no permitidos y cambiar el signo - al comienzo para
  463.    //  que no de error de conversi≤n
  464.    i := 1;
  465.    while i <= Length(Text) do
  466.     if not (Text[i] in ['0'..'9',',','.','-'])
  467.      then Text := Copy(Text,1,i-1) + Copy(Text,i+1,Length(Text)-i)
  468.      else i := i + 1;
  469.  
  470.    // Si solo tenemos el signo negativo, darφa error
  471.    if (Pos('-',Text) = 1) and (Length(Text) = 1) then Exit;
  472.  
  473.    // Temporary variable to accomplish the conversion
  474.    TxtConvert := Text;
  475.  
  476.    // To put the negative sign to the beginning
  477.    if (EditType <> etString) and (Pos('-',TxtConvert) > 1) then
  478.     if Length(TxtConvert) = Pos('-',TxtConvert)
  479.      then TxtConvert := '-' + Copy(TxtConvert,1,Pos('-',TxtConvert)-1)
  480.      else TxtConvert := '-' +
  481.                         Copy(TxtConvert,1,Pos('-',TxtConvert)-1) +
  482.                         Copy(TxtConvert,Pos('-',TxtConvert) + 1,Length(TxtConvert) - Pos('-',TxtConvert));
  483.  
  484.    if EditType = etInteger then begin
  485.     // Range control of Integer
  486.     ValTemp := StrToFloat(TxtConvert);
  487.     if (ValTemp > 2147483647) or (ValTemp < -2147483647) then begin
  488.      ShowMessage('Range Max. : -2147483647 <-> 2147483647');
  489.      ValueInteger := 0;
  490.     end else begin
  491.      ValueInteger := StrToInt(TxtConvert);
  492.      ValueFloat   := StrToFloat(TxtConvert + sDecimal + '0'); {New in Version 2.0}
  493.     end;
  494.    end;
  495.  
  496.    // El tipo Float - Double, permite valores hasta 5.0 * 10e-324 .. 1.7 * 10e308
  497.    // con 15-16 digitos significativos, por lo que solamente controlamos que el total
  498.    // no pase de 16 digitos. Hasta la fecha no he experimentado con valores Float tan
  499.    // altos, por lo que no me atrevo a condicionar algo que no conozco con exactitud.}
  500.    if EditType = etFloat then begin
  501.     ValueFloat   := StrToFloat(TxtConvert);
  502.     ValueInteger := Trunc(ValueFloat);
  503.    end;
  504.  
  505.   except
  506.    on EConvertError do begin
  507.      ShowMessage('Range Max. :' + #13 +
  508.                  ' - Integer : -2147483647 <-> 2147483647' + #13 +
  509.                  ' - Float   : 5.0e-324 <-> 1.7e+308');
  510.      ValueInteger := 0;
  511.      ValueFloat   := 0;
  512.    end;
  513.   end;
  514.  end;
  515.  
  516.  if Assigned(FOnChange) then FOnChange(Self);
  517. end;
  518.  
  519. procedure TEditN.SetInteger(VInteger : Integer);
  520. begin
  521.  if EditType = etInteger then Text := IntToStr(VInteger);
  522. end;
  523.  
  524. procedure TEditN.SetFloat(VFloat : Double);
  525. begin
  526.  if EditType = etFloat then Text := FloatToStr(VFloat);
  527. end;
  528.  
  529. procedure TEditN.FormatDate;
  530. var
  531.  Temp,vDate,vMonth,vYear : String;
  532.  dDate   : TDateTime;
  533.  ilength : Integer;
  534. begin
  535.  // Decode the Date
  536.  Temp    := '';
  537.  vDate   := FormatDateTime('dd' + sDate + 'mm' + sDate + 'yyyy',Date);
  538.  vMonth  := Copy(vDate,4,2);
  539.  vYear   := Copy(vDate,7,4);
  540.  
  541.  // Quitar separador de fecha si existe
  542.  if Length(Text) > 0 then
  543.   for iLength := 1 to Length(Text) do
  544.    if Text[iLength] in ['0'..'9']
  545.     then Temp := Temp + Text[iLength];
  546.  
  547.  // Completar la fecha con separadores
  548.  iLength := Length(Temp);
  549.  Case iLength of
  550.   0 : Temp := vDate;
  551.   1 : Temp := '0' + Temp[1] + sDate + vMonth + sDate + vYear;
  552.   2 : Temp := Temp + sDate + vMonth + sDate + vYear;
  553.   3 : Temp := Copy(Temp,1,2) + sDate + '0' + Temp[3] + sDate + vYear;
  554.   4 : Temp := Copy(Temp,1,2) + sDate + Copy(Temp,3,2) + sDate + vYear;
  555.   5 : Temp := Copy(Temp,1,2) + sDate + Copy(Temp,3,2) + sDate + Copy(vYear,1,3) + Temp[5];
  556.   6 : Temp := Copy(Temp,1,2) + sDate + Copy(Temp,3,2) + sDate + Copy(vYear,1,2) + Copy(Temp,5,2);
  557.   7 : Temp := Copy(Temp,1,2) + sDate + Copy(Temp,3,2) + sDate + vYear[1] + Copy(Temp,5,3);
  558.   8,9,10 : Temp := Copy(Temp,1,2) + sDate + Copy(Temp,3,2) + sDate + Copy(Temp,5,4);
  559.  end;
  560.  
  561.  // Test of correct Date
  562.  try
  563.   dDate := StrToDate(Temp);
  564.  except
  565.   ShowMessage('Date incorrect');
  566.   // On error, the Date is actually for default
  567.   ValueDate    := Date;
  568.   ValueFloat   := Date; // TDateTime : Double;
  569.   ValueInteger := Trunc(Date);
  570.   Exit;
  571.  end;
  572.  
  573.  // The Date is correct. Assign value
  574.  Text         := Temp;
  575.  ValueDate    := StrToDate(Temp);
  576.  ValueFloat   := ValueDate; // TDateTime : Double;
  577.  ValueInteger := Trunc(ValueDate);
  578. end;
  579.  
  580. procedure TEditN.FormatTime;
  581. var
  582.  Temp,vTime,vMin,vSec,MskTime : String;
  583.  iLength : Integer;
  584.  tTime   : TDateTime;
  585. begin
  586.  Temp    := '';
  587.  MskTime := '00' + sTime + '00' + sTime + '00';
  588.  vTime   := FormatDateTime('hh:mm:ss',Time);
  589.  vMin    := Copy(vTime,4,2);
  590.  vSec    := Copy(vTime,7,2);
  591.  
  592.  // Quitar separadores si los hay
  593.  if Length(Text) > 0 then
  594.   for iLength := 1 to Length(Text) do
  595.    if Text[iLength] in ['0'..'9'] then Temp := Temp + Text[iLength];
  596.  
  597.  // Formatear el tiempo
  598.  iLength := Length(Temp);
  599.  if TimeSeconds then begin // Con segundos
  600.    Case iLength of
  601.     0 : Temp := vTime;
  602.     1 : Temp := '0' + Temp[1] + Copy(MskTime,3,6);
  603.     2 : Temp := Temp + Copy(MskTime,3,6);
  604.     3 : Temp := Copy(Temp,1,2) + sTime + '0' + Temp[3] + Copy(MskTime,6,3);
  605.     4 : Temp := Copy(Temp,1,2) + sTime  + Copy(Temp,3,2) + Copy(MskTime,6,3);
  606.     5 : Temp := Copy(Temp,1,2) + sTime  + Copy(Temp,3,2) + sTime + '0' + Temp[5];
  607.     6,7,8 : Temp := Copy(Temp,1,2) + sTime  + Copy(Temp,3,2) + sTime + Copy(Temp,5,2);
  608.    end;
  609.  end else begin // Sin segundos
  610.    Case iLength of
  611.     0 : Temp := vTime;
  612.     1 : Temp := '0' + Temp[1] + Copy(MskTime,3,3);
  613.     2 : Temp := Temp + Copy(MskTime,3,3);
  614.     3 : Temp := Copy(Temp,1,2) + sTime + '0' + Temp[3];
  615.     4,5 : Temp := Copy(Temp,1,2) + sTime  + Copy(Temp,3,2);
  616.    end;
  617.  end;
  618.  
  619.  // Test of string-time
  620.  try
  621.   tTime := StrToTime(Temp);
  622.  except
  623.   ShowMessage('Time incorrect');
  624.   if TimeSeconds then Text := vTime else Text := Copy(vTime,1,5);
  625.   ValueTime  := Time;
  626.   ValueFloat := ValueTime;
  627.   Exit;
  628.  end;
  629.   // The time is correct
  630.   Text       := Temp;
  631.   ValueTime  := StrToTime(Temp);
  632.   ValueFloat := ValueTime;
  633. end;
  634.  
  635. {***************************************************************************}
  636. constructor TMEditN.Create(AOwner : TComponent);
  637. begin
  638.  inherited Create(AOwner);
  639.  ColorOnFocus        := clWhite;
  640.  ColorOnNotFocus     := clSilver;
  641.  Color               := ColorOnNotFocus;
  642.  FontColorOnFocus    := clRed;
  643.  FontColorOnNotFocus := clBlack;
  644.  FWidthOnFocus       := 0;
  645.  FKeyTab             := #9;
  646. end;
  647.  
  648. procedure TMEditN.KeyPress(var Key: Char);
  649. var
  650.  {$IFDEF VER80}
  651.   FEditTemp : TForm;       {For Delphi 1}
  652.  {$ENDIF}
  653.  
  654.  {$IFDEF VER90}
  655.   FEditTemp : TForm;       {For Delphi 2}
  656.  {$ENDIF}
  657.  
  658.  {$IFDEF VER100}
  659.   FEditTemp : TCustomForm; {For Delphi 3}
  660.  {$ENDIF}
  661.  
  662.  {$IFDEF VER120}
  663.   FEditTemp : TCustomForm; {For Delphi 4}
  664.  {$ENDIF}
  665.  
  666. begin
  667.  if Key = EditKeyByTab then begin
  668.   FEditTemp := GetParentForm(Self);
  669.   SendMessage(FEditTemp.Handle, WM_NEXTDLGCTL, 0, 0);
  670.   Key := #0;
  671.  end;
  672.  
  673.  if Key <> #0 then inherited KeyPress(Key);
  674. end;
  675.  
  676. procedure TMEditN.DoEnter;
  677. begin
  678.  // To assign the Color upon receiving the focus
  679.  Color       := ColorOnFocus;
  680.  Font.Color  := FontColorOnFocus;
  681.  if WidthOnFocus > 0 then begin
  682.   iWidth := Width;
  683.   Width  := FWidthOnFocus;
  684.  end;
  685.  
  686.  if Assigned(FOnEnter) then FOnEnter(Self);
  687. end;
  688.  
  689. procedure TMEditN.DoExit;
  690. begin
  691.  // To return the color of the fund upon leaving and losing the focus
  692.  Color      := ColorOnNotFocus;
  693.  Font.Color := FontColorOnNotFocus;
  694.  if WidthOnFocus > 0 then Width := iWidth;
  695.  
  696.  if Assigned(FOnExit) then FOnExit(Self);
  697. end;
  698.  
  699. {***************************************************************************}
  700. constructor TDBEditN.Create(AOwner : TComponent);
  701. begin
  702.   inherited Create(AOwner);
  703.   ColorOnFocus        := clWhite;
  704.   ColorOnNotFocus     := clSilver;
  705.   Color               := ColorOnNotFocus;
  706.   FontColorOnFocus    := clRed;
  707.   FontColorOnNotFocus := clBlack;
  708.   FUpper              := False;
  709.   FUpperList          := ' (';
  710.   FWidthOnFocus       := 0;
  711.   FKeyTab             := #9;
  712. end;
  713.  
  714. procedure TDBEditN.KeyPress(var Key: Char);
  715. var
  716.  {$IFDEF VER80}
  717.   FEditTemp : TForm;       {For Delphi 1}
  718.  {$ENDIF}
  719.  
  720.  {$IFDEF VER90}
  721.   FEditTemp : TForm;       {For Delphi 2}
  722.  {$ENDIF}
  723.  
  724.  {$IFDEF VER100}
  725.   FEditTemp : TCustomForm; {For Delphi 3}
  726.  {$ENDIF}
  727.  
  728.  {$IFDEF VER120}
  729.   FEditTemp : TCustomForm; {For Delphi 4}
  730.  {$ENDIF}
  731.  
  732.  c : String;
  733. begin
  734.  if Key = EditKeyByTab then begin
  735.   FEditTemp := GetParentForm(Self);
  736.   SendMessage(FEditTemp.Handle, WM_NEXTDLGCTL, 0, 0);
  737.   Key := #0;
  738.  end;
  739.  
  740.  if FUpper then begin // Capital letter  - Ma²usculas
  741.   if (Length(Text) = 0) or
  742.      (SelText = Text) or
  743.      (Pos(Text[Length(Text)],FUpperList) > 0) then begin
  744.    C   := AnsiUpperCase(Key);
  745.    Key := C[1];
  746.   end;
  747.  end;
  748.  
  749.  if Key <> #0 then inherited KeyPress(Key);
  750. end;
  751.  
  752. procedure TDBEditN.DoEnter;
  753. begin
  754.  // To assign the Color upon receiving the focus
  755.  Color       := ColorOnFocus;
  756.  Font.Color  := FontColorOnFocus;
  757.  if WidthOnFocus > 0 then begin
  758.   iWidth := Width;
  759.   Width  := FWidthOnFocus;
  760.  end;
  761.  
  762.  if Assigned(FOnEnter) then FOnEnter(Self);
  763. end;
  764.  
  765. procedure TDBEditN.DoExit;
  766. begin
  767.  // To return the color of the back upon leaving and losing the focus
  768.  Color      := ColorOnNotFocus;
  769.  Font.Color := FontColorOnNotFocus;
  770.  if WidthOnFocus > 0 then Width := iWidth;
  771.  
  772.  if Assigned(FOnExit) then FOnExit(Self);
  773. end;
  774.  
  775. procedure Register;
  776. begin
  777.   RegisterComponents('Standard', [TEditN]);
  778.   RegisterComponents('Additional', [TMEditN]);
  779.   RegisterComponents('Data Controls', [TDBEditN]);
  780. end;
  781.  
  782. end.
  783.