[go: up one dir, main page]

Menu

[r10]: / Componentes / Fecha.pas  Maximize  Restore  History

Download this file

146 lines (126 with data), 3.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
unit Fecha;
interface
uses
SysUtils, Classes, Controls, StdCtrls;
type
TFecha = class(TEdit)
private
FFecha: TDate;
procedure SetDate(dteFecha: TDate);
procedure CMExit(var Message: TCMExit); message CM_EXIT;
protected
procedure FormatText; dynamic;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
public
constructor Create(AOwner: TComponent); override;
published
property Anchors;
property AutoSize;
property BorderStyle;
property Color;
property Ctl3D;
property Date: TDate read FFecha write SetDate;
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;
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;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TFecha]);
end;
{ TFecha }
procedure TFecha.CMExit(var Message: TCMExit);
begin
inherited;
FormatText;
end;
constructor TFecha.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFecha:= 0;
end;
procedure TFecha.FormatText;
begin
if(FFecha <> 0) then
Text:= FormatDateTime(ShortDateFormat, FFecha)
else
Text:= EmptyStr;
end;
procedure TFecha.KeyUp(var Key: Word; Shift: TShiftState);
var
dteFecha: TDateTime;
iDia: integer;
iMes: integer;
iAnio: integer;
iTam: integer;
sFecha: String;
begin
inherited;
sFecha:= Text;
if(Trim(sFecha) = EmptyStr) then
begin
FFecha:= 0;
exit;
end;
// si es una fecha válida, no hacer nada
if(TryStrToDate(sFecha, dteFecha)) then
begin
FFecha:= dteFecha;
exit;
end;
iTam:= Length(sFecha);
// Si no tiene diagonales
if(Pos('/', sFecha) = 0) then
begin
iDia:= 0;
iMes:= 0;
iAnio:= 0;
TryStrToInt(Copy(sFecha, 1, 2), iDia);
TryStrToInt(Copy(sFecha, 3, 2), iMes);
TryStrToInt(Copy(sFecha, 5, 4), iAnio);
if(iTam in [1, 2]) then
sFecha:= IntToStr(iDia) + FormatDateTime('/mm/yyyy', Now)
else if(iTam in [3, 4]) then
sFecha:= IntToStr(iDia) + '/' + IntToStr(iMes) + FormatDateTime('/yyyy', Now)
else if(iTam in [5..8]) then
sFecha:= IntToStr(iDia) + '/' + IntToStr(iMes) + '/' + IntToStr(iAnio);
end;
// si no es una fecha válida, poner la fecha actual
if(not TryStrToDate(sFecha, dteFecha)) then
FFecha:= Date
else
FFecha:= dteFecha;
end;
procedure TFecha.SetDate(dteFecha: TDate);
begin
FFecha:= dteFecha;
FormatText;
end;
end.