Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 28-12-2006
Avatar de gluglu
[gluglu] gluglu is offline
Miembro Premium
 
Registrado: sep 2004
Ubicación: Málaga - España
Posts: 1.455
Poder: 21
gluglu Va por buen camino
Validar y Autocompletar Fecha en un DBEdit

Hola Compañeros del Foro !

Intento validar y autocompletar una fecha dentro de un TDBEdit. Para ello he hecho uso del método descrito en http://delphi.about.com/od/adptips20...ltip0305_3.htm

Ligeramente modificado, me ha quedado el siguiente componente denominado TDBDateEdit

Código Delphi [-]
unit DBDateEdit;
 
interface
 
uses
  SysUtils, Classes, System.ComponentModel, Borland.Vcl.Controls,
  Borland.Vcl.StdCtrls, Borland.Vcl.Mask, Borland.Vcl.DBCtrls, DateUtils;
 
type
  TDBDateEdit = class(TDBEdit)
  private
    { Private declarations }
    procedure DateComplete;
  protected
    { Protected declarations }
  public
    { Public declarations }
    procedure ValidateEdit; override;
  published
    { Published declarations }
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterComponents('Data Controls', [TDBDateEdit]);
end;
 
procedure TDBDateEdit.DateComplete;
var
  DateStr : string;
  Year, Month, Day : Word;
  cnt, SepCount : Integer;
begin
 
  DateStr := Text;
 
  if DateStr = '' then Exit;
  SepCount := 0;
 
  for cnt := 1 to Length(DateStr) do begin
    if not (DateStr[cnt] in ['0'..'9']) then begin
      DateStr[cnt] := '/';
      Inc(SepCount);
    end;
  end;
 
  while (DateStr <> '') and (DateStr[Length(DateStr)] = '/') do begin
    Delete(DateStr, Length(DateStr), 1);
    Dec(SepCount);
  end;
 
  DecodeDate(Now, Year, Month, Day);
 
  if SepCount = 0 then begin
    case Length(DateStr) of
         0 : DateStr := DateToStr(Now);
      1, 2 : DateStr := DateStr + '/' + IntToStr(Month);
         4 : Insert('/', DateStr, 3) ;
      6, 8 : begin
               Insert('/', DateStr, 5) ;
               Insert('/', DateStr, 3) ;
             end;
    end;
  end;
 
  try
    Text := DateToStr(StrToDate(DateStr));
  except
    abort;
  end;
 
end;
 
procedure TDBDateEdit.ValidateEdit;
var
   pos : integer;
begin
 
   if not Validate(Text, pos) then
     DateComplete;
 
   inherited;
 
end;
 
end.

Necesito un achuchón final.

El autocompletar funciona a la perfección. Pero me sigue saltando un error de 'Invalid Date' si introduzco p.ej. 29/02/2006. En cambio funciona bien, abortando la operación, si p.ej. introduzco en la fecha '89' como día.

Por otro lado también me gustaría poder llamar desde algún lado a mi propia rutina de mensaje de error que se encuentra en el programa principal en el cual hago uso del componente modificado, indicando p.ej. en el caso anterior de que se introdujera 29 de Febrero de 2006, u '89' en el día, que la fecha introducida es erronea.

Donde debería llamar a dicha rutina propia, o qué valor debería devolver en el componente para interceptar algún evento en mi programa principal ?

Como siempre, gracias por vuestra ayuda, y como no, aprovechar esta ocasión para desearos a todos Felices Fiestas y un Próspero Año Nuevo 2007.
__________________
Piensa siempre en positivo !
Responder Con Cita
  #2  
Antiguo 28-12-2006
Avatar de gluglu
[gluglu] gluglu is offline
Miembro Premium
 
Registrado: sep 2004
Ubicación: Málaga - España
Posts: 1.455
Poder: 21
gluglu Va por buen camino
Pensaba que al introducir '29/02/2006' me ejecutaba
Código Delphi [-]
procedure TDBDateEdit.ValidateEdit;
var
   pos : integer;
   Aux_Date : TDateTime;
begin
   if not Validate(Text, pos) then
     DateComplete;
  ...

... pero estaba equivocado, y no entra ahí.

Funciona correctamente así :
Código Delphi [-]
procedure TDBDateEdit.ValidateEdit;
var
   pos : integer;
   Aux_Date : TDateTime;
begin
 
   if not Validate(Text, pos) then
     DateComplete;
 
   if not TryStrToDate(Text, Aux_Date) then
     Abort;
   
   inherited;
 
end;

Me queda entonces sólo la pregunta acerca de donde interceptar un posible error en mi programa principal para lanzar mi propio mensaje de error avisando de Fecha Erronea.

Entiendo que tengo que cambiar el 'Abort' por algo, pero necesito vuestra ayuda.
__________________
Piensa siempre en positivo !
Responder Con Cita
  #3  
Antiguo 28-12-2006
Avatar de Lepe
[Lepe] Lepe is offline
Miembro Premium
 
Registrado: may 2003
Posts: 7.424
Poder: 29
Lepe Va por buen camino
Creamos un evento para que el usuario del componente pueda asignarlo via el inspector de objetos, lo llamamos OnValidateError.

Después de lanzar el evento dentro de nuestro componente, llamamos a Abort, para que continue.

Código Delphi [-]
unit DBDateEdit;
 
interface
 
uses
  SysUtils, Classes, System.ComponentModel, Borland.Vcl.Controls,
  Borland.Vcl.StdCtrls, Borland.Vcl.Mask, Borland.Vcl.DBCtrls, DateUtils;
 
type
  TError = ( eDay, eMonth, eYear, eDayAndMonth, eDayAndYear, eMonthAndYear, eAll);
  TValidateError = procedure (Sender:TObject; ErrorOn:TError; CurrentDate:string) of object;

  TDBDateEdit = class(TDBEdit)
  private
    { Private declarations }
    FOnError: TValidateError;
    procedure DateComplete;
  protected
    { Protected declarations }
  public
    { Public declarations }
    procedure ValidateEdit; override;
  published
    { Published declarations }
    property OnValidateError :TVAlidateError read FOnError write FOnError;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterComponents('Data Controls', [TDBDateEdit]);
end;
 
procedure TDBDateEdit.DateComplete;
var
  DateStr : string;
  Year, Month, Day : Word;
  cnt, SepCount : Integer;
  tmpDate:TDatetime;
begin
 
  DateStr := Text;
 
  if DateStr = '' then Exit;
  SepCount := 0;
 
  for cnt := 1 to Length(DateStr) do begin
    if not (DateStr[cnt] in ['0'..'9']) then begin
      DateStr[cnt] := '/';
      Inc(SepCount);
    end;
  end;
 
  while (DateStr <> '') and (DateStr[Length(DateStr)] = '/') do begin
    Delete(DateStr, Length(DateStr), 1);
    Dec(SepCount);
  end;
 
  DecodeDate(Now, Year, Month, Day);
 
  if SepCount = 0 then begin
    case Length(DateStr) of
         0 : DateStr := DateToStr(Now);
      1, 2 : DateStr := DateStr + '/' + IntToStr(Month);
         4 : Insert('/', DateStr, 3) ;
      6, 8 : begin
               Insert('/', DateStr, 5) ;
               Insert('/', DateStr, 3) ;
             end;
    end;
  end;
 
  if not tryStrtoDate(year, month, day, tmpDatetime) then
  begin
    if Assigned(FOnError) then
    // tocaría averiguar donde falla la fecha y después asignar el parámetro ErrorOn
        FOnError(self, eDay, Datestr);
    abort;
  end;
end;
 
procedure TDBDateEdit.ValidateEdit;
var
   pos : integer;
begin
 
   if not Validate(Text, pos) then
     DateComplete;
 
   inherited;
 
end;
 
end.

Tendrás que añadir el Uses DateUtils para que reconozca TryStrToDate. Además está hecho de memoria, así que puede tener algún error en el orden de parámetros al llamar a dicha función.

Saludos
__________________
Si usted entendió mi comentario, contácteme y gustosamente,
se lo volveré a explicar hasta que no lo entienda, Gracias.
Responder Con Cita
  #4  
Antiguo 28-12-2006
Avatar de gluglu
[gluglu] gluglu is offline
Miembro Premium
 
Registrado: sep 2004
Ubicación: Málaga - España
Posts: 1.455
Poder: 21
gluglu Va por buen camino
Perfesssto !

Muy agradecido.

Lo he dejado de la siguiente manera :

Código Delphi [-]
unit DBDateEdit;
 
interface
 
uses
  SysUtils, Classes, System.ComponentModel, Borland.Vcl.Controls,
  Borland.Vcl.StdCtrls, Borland.Vcl.Mask, Borland.Vcl.DBCtrls;
 
type
  TError = ( eDay, eMonth, eYear, eDayAndMonth, eDayAndYear, eMonthAndYear, eAll);
  TValidateError = procedure (Sender: TObject; ErrorOn: TError; CurrentDate: string) of object;
  TDBDateEdit = class(TDBEdit)
  private
    { Private declarations }
    FOnError: TValidateError;
    procedure DateComplete;
  protected
    { Protected declarations }
  public
    { Public declarations }
    procedure ValidateEdit; override;
  published
    { Published declarations }
    property OnValidateError: TValidateError read FOnError write FOnError;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterComponents('Data Controls', [TDBDateEdit]);
end;
 
procedure TDBDateEdit.DateComplete;
var
  DateStr : string;
  Year, Month, Day : Word;
  cnt, SepCount : Integer;
begin
 
  DateStr := Text;
 
  if DateStr = '' then Exit;
  SepCount := 0;
 
  for cnt := 1 to Length(DateStr) do begin
    if not (DateStr[cnt] in ['0'..'9']) then begin
      DateStr[cnt] := '/';
      Inc(SepCount);
    end;
  end;
 
  while (DateStr <> '') and (DateStr[Length(DateStr)] = '/') do begin
    Delete(DateStr, Length(DateStr), 1);
    Dec(SepCount);
  end;
 
  DecodeDate(Now, Year, Month, Day);
 
  if SepCount = 0 then begin
    case Length(DateStr) of
         0 : DateStr := DateToStr(Now);
      1, 2 : DateStr := DateStr + '/' + IntToStr(Month);
         4 : Insert('/', DateStr, 3) ;
      6, 8 : begin
               Insert('/', DateStr, 5) ;
               Insert('/', DateStr, 3) ;
             end;
    end;
  end;
 
  try
    Text := DateToStr(StrToDate(DateStr));
  except
    if Assigned(FOnError) then FOnError(self, eDay, '');
    Abort;
  end;
 
end;
 
procedure TDBDateEdit.ValidateEdit;
var
  pos : integer;
  Aux_Date : TDateTime;
begin
 
  if not Validate(Text, pos) then
     DateComplete
  else begin
 
    try
      Text := DateToStr(StrToDate(Text));
    except
      if Assigned(FOnError) then FOnError(self, eDay, '');
      Abort;
    end;
 
    inherited;
 
  end;
 
end;
 
end.

Esto me creo el evento deseado OnValidateError y en él puedo ya llamar a mi propia rutina del programa principal de mensaje de error.

De nuevo, muchas gracias Lepe.
__________________
Piensa siempre en positivo !
Responder Con Cita
  #5  
Antiguo 29-12-2006
Avatar de Lepe
[Lepe] Lepe is offline
Miembro Premium
 
Registrado: may 2003
Posts: 7.424
Poder: 29
Lepe Va por buen camino
Hombreeeee, pero cúrratelo un poco más jajajaja.

El parámetro ErrorOn está pensado para decirle al programador (ususario del TDBedit) qué falla en la fecha, si el día (es mayor de 31), el mes (si es mayor de 12), si el día con el més (por ejemplo fallo del 31/02/2003) ya que febrero no tiene 31, etc.

Si no quieres usar ese parámetro para nada, borralo de su definición y borra también el parámetro cuando lanzas el evento.

Saludos
__________________
Si usted entendió mi comentario, contácteme y gustosamente,
se lo volveré a explicar hasta que no lo entienda, Gracias.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Validar un Dbedit? kman Varios 10 13-03-2007 04:07:11
Validar un DBEdit con delphi interbase servicomp Conexión con bases de datos 8 06-08-2006 16:27:11
validar dbedit yeison Cristman Conexión con bases de datos 1 29-07-2006 21:55:56
validar en fecha, dia y mes alcides Varios 2 08-02-2005 16:29:33
como validar datos en el evento onkeypress de un dbedit? viajero2015 Varios 1 01-02-2004 21:41:59


La franja horaria es GMT +2. Ahora son las 01:46:39.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi