-4

我有

var objeto:TDateEdit; ...

如果我更改as TDateEdit代码:

if UpperCase(CAMPOSnoBANCO[ii]) = UpperCase((FORMULARIO.Components[i] as TDateEdit).name )

并使用这个:

if UpperCase(CAMPOSnoBANCO[ii]) = UpperCase((FORMULARIO.Components[i] as objeto).name)

代码有效....但我需要改变

objeto := TDBEdit;

我需要一种方法来更改此变量类型以减少我将使用的代码

If (FORMULARIO.Components[i] is TDateEdit) then objeto := TDateEdit

If (FORMULARIO.Components[i] is TDBLookupComboBox) then objeto := TDBLookupComboBox

但变量是

var objeto: TDateEdit;

如何在不重复每种对象类型的代码的情况下做到这一点?

Function ValidaCampoObrigatorio(): boolean;
    var i,PrimeiroCampo,ii,indexOfItem:Integer;
    var MSGaglutinada,TIPO:string;
    var NOMEcampo : variant ;
       var objeto:TDateEdit ;
    var VALIDOU:boolean;
    
    
    
    begin
         CorERRO:=RGB(255,218,185);
         CorCertO:= 12582911;//$FF000005;
    
              
    
    Result := false;
    MSGaglutinada:='';
    PrimeiroCampo:=-1;
    tipo:='';
    NOMEcampo:='';
    
     for i := 0 to FORMULARIO.ComponentCount -1 do
      begin
    
    
      if (FORMULARIO.Components[i] is TDateEdit) then
       begin
    
          indexOfItem := -1;
          ii:=0;
          for ii := Low(CAMPOSnoBANCO) to High(CAMPOSnoBANCO) do
          //LOCALIZANDO DENTRO da matriz
           if UpperCase(CAMPOSnoBANCO[ii]) = UpperCase((FORMULARIO.Components[i] as TDateEdit).name ) then
             begin
             BREAK;
             end;
             VALIDOU:=True;
           if EhRequerido[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)]=true then
             begin
                 if((FORMULARIO.Components[i] as TDateEdit).text = '')then
                 begin
                    if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDateEdit' end;
                     MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDateEdit).Name)+' não pode ser NULO'+ #13#10+Trim(MSGaglutinada)   ;
                     (FORMULARIO.Components[i] as TDateEdit).Color:=corERRO; VALIDOU:=false ;
                 end;
    
             end;
           if EhEMAIL[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)]=true  then
            begin
              if ValidEmail((FORMULARIO.Components[i] as TDateEdit).text) = False  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDateEdit).Name)+' DEVE ser válido'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDateEdit).Color:=corERRO; VALIDOU:=false ;
               end;
            end ;
           if EhData[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)]=true  then
            begin
              if DataValida((FORMULARIO.Components[i] as TDateEdit).text)=false  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDateEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDateEdit).Name)+' DEVE uma data Válida'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDateEdit).Color:=corERRO; VALIDOU:=false ;
               end
               else
              begin
              if (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)])<=StrToDate((FORMULARIO.Components[i] as TDateEdit).text)then
                   begin
    
                         if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDateEdit' end;
                          MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDateEdit).Name)+' não pode maior ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                          (FORMULARIO.Components[i] as TDateEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
              end;
              if (OperadorDataMaior[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamaior[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)])>=StrToDate((FORMULARIO.Components[i] as TDateEdit).text)then
                   begin
    
                           if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDateEdit' end;
                            MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDateEdit).Name)+' não pode menor ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDateEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                            (FORMULARIO.Components[i] as TDateEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
               end;
    
    
               end;
    
    
    
    
            end ;
    
    
    
    
           if VALIDOU=true then
            begin
                      (FORMULARIO.Components[i] as TDateEdit).Color:= CorCertO;
            end;
           end;
    
    
    
    
    
    
      if (FORMULARIO.Components[i] is TDBEdit) then
       begin
    
          indexOfItem := -1;
          ii:=0;
          for ii := Low(CAMPOSnoBANCO) to High(CAMPOSnoBANCO) do
          //LOCALIZANDO DENTRO da matriz
           if UpperCase(CAMPOSnoBANCO[ii]) = UpperCase((FORMULARIO.Components[i] as TDBEdit).name ) then
             begin
             BREAK;
             end;
             VALIDOU:=True;
           if EhRequerido[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)]=true then
             begin
                 if((FORMULARIO.Components[i] as TDBEdit).field.AsString = '')then
                 begin
                    if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBEdit' end;
                     MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBEdit).Name)+' não pode ser NULO'+ #13#10+Trim(MSGaglutinada)   ;
                     (FORMULARIO.Components[i] as TDBEdit).Color:=corERRO; VALIDOU:=false ;
                 end;
    
             end;
           if EhEMAIL[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)]=true  then
            begin
              if ValidEmail((FORMULARIO.Components[i] as TDBEdit).text) = False  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDBEdit).Name)+' DEVE ser válido'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDBEdit).Color:=corERRO; VALIDOU:=false ;
               end;
            end ;
           if EhData[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)]=true  then
            begin
             if DataValida((FORMULARIO.Components[i] as TDBEdit).text)=false  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDBEdit).Name)+' DEVE uma data Válida'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDBEdit).Color:=corERRO; VALIDOU:=false ;
               end
               else
              begin
              if (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)])<=StrToDate((FORMULARIO.Components[i] as TDBEdit).text)then
                   begin
    
                         if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBEdit' end;
                          MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBEdit).Name)+' não pode maior ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                          (FORMULARIO.Components[i] as TDBEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
              end;
              if (OperadorDataMaior[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamaior[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)])>=StrToDate((FORMULARIO.Components[i] as TDBEdit).text)then
                   begin
    
                           if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBEdit' end;
                            MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBEdit).Name)+' não pode menor ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                            (FORMULARIO.Components[i] as TDBEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
               end;
    
    
               end;
    
            end ;
    
    
    
    
           if VALIDOU=true then
            begin
                      (FORMULARIO.Components[i] as TDBEdit).Color:= CorCertO;
            end;
           end;
    
    
    
    
           if (FORMULARIO.Components[i] is TDBLookupComboBox) then
       begin
    
          indexOfItem := -1;
          ii:=0;
          for ii := Low(CAMPOSnoBANCO) to High(CAMPOSnoBANCO) do
          //LOCALIZANDO DENTRO da matriz
           if UpperCase(CAMPOSnoBANCO[ii]) = UpperCase((FORMULARIO.Components[i] as TDBLookupComboBox).name ) then
             begin
             BREAK;
             end;
             VALIDOU:=True;
           if EhRequerido[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)]=true then
             begin
                 if((FORMULARIO.Components[i] as TDBLookupComboBox).field.AsString = '')then
                 begin
                    if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBLookupComboBox' end;
                     MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBLookupComboBox).Name)+' não pode ser NULO'+ #13#10+Trim(MSGaglutinada)   ;
                     (FORMULARIO.Components[i] as TDBLookupComboBox).Color:=corERRO; VALIDOU:=false ;
                 end;
    
             end;
           if EhEMAIL[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)]=true  then
            begin
              if ValidEmail((FORMULARIO.Components[i] as TDBLookupComboBox).text) = False  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBLookupComboBox' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDBLookupComboBox).Name)+' DEVE ser válido'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDBLookupComboBox).Color:=corERRO; VALIDOU:=false ;
               end;
            end ;
    
           if EhData[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)]=true  then
            begin
              if DataValida((FORMULARIO.Components[i] as TDBLookupComboBox).text)=false  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBLookupComboBox' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDBLookupComboBox).Name)+' DEVE uma data Válida'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDBLookupComboBox).Color:=corERRO; VALIDOU:=false ;
               end
               else
              begin
              if (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)])<=StrToDate((FORMULARIO.Components[i] as TDBLookupComboBox).text)then
                   begin
    
                         if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBLookupComboBox' end;
                          MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBLookupComboBox).Name)+' não pode maior ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                          (FORMULARIO.Components[i] as TDBLookupComboBox).Color:=corERRO; VALIDOU:=false ;
    
                   end;
              end;
              if (OperadorDataMaior[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamaior[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)])>=StrToDate((FORMULARIO.Components[i] as TDBLookupComboBox).text)then
                   begin
    
                           if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBLookupComboBox' end;
                            MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBLookupComboBox).Name)+' não pode menor ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBLookupComboBox).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                            (FORMULARIO.Components[i] as TDBLookupComboBox).Color:=corERRO; VALIDOU:=false ;
    
                   end;
               end;
    
    
               end;
    
            end ;
    
    
    
    
           if VALIDOU=true then
            begin
                      (FORMULARIO.Components[i] as TDBLookupComboBox).Color:= CorCertO;
            end;
           end;
    
    
    
    
           if (FORMULARIO.Components[i] is TDBDateEdit) then
       begin
    
          indexOfItem := -1;
          ii:=0;
          for ii := Low(CAMPOSnoBANCO) to High(CAMPOSnoBANCO) do
          //LOCALIZANDO DENTRO da matriz
           if UpperCase(CAMPOSnoBANCO[ii]) = UpperCase((FORMULARIO.Components[i] as TDBDateEdit).name ) then
             begin
             BREAK;
             end;
             VALIDOU:=True;
           if EhRequerido[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)]=true then
             begin
                 if((FORMULARIO.Components[i] as TDBDateEdit).text = '')then
                 begin
                    if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBDateEdit' end;
                     MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBDateEdit).Name)+' não pode ser NULO'+ #13#10+Trim(MSGaglutinada)   ;
                     (FORMULARIO.Components[i] as TDBDateEdit).Color:=corERRO; VALIDOU:=false ;
                 end;
    
             end;
           if EhEMAIL[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)]=true  then
            begin
              if ValidEmail((FORMULARIO.Components[i] as TDBDateEdit).text) = False  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBDateEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDBDateEdit).Name)+' DEVE ser válido'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDBDateEdit).Color:=corERRO; VALIDOU:=false ;
               end;
            end ;
    
           if EhData[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)]=true  then
            begin
              if DataValida((FORMULARIO.Components[i] as TDBDateEdit).text)=false  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBDateEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TDBDateEdit).Name)+' DEVE uma data Válida'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TDBDateEdit).Color:=corERRO; VALIDOU:=false ;
               end
               else
              begin
              if (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)])<=StrToDate((FORMULARIO.Components[i] as TDBDateEdit).text)then
                   begin
    
                         if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBDateEdit' end;
                          MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBDateEdit).Name)+' não pode maior ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                          (FORMULARIO.Components[i] as TDBDateEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
              end;
              if (OperadorDataMaior[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamaior[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)])>=StrToDate((FORMULARIO.Components[i] as TDBDateEdit).text)then
                   begin
    
                           if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBDateEdit' end;
                            MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TDBDateEdit).Name)+' não pode menor ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TDBDateEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                            (FORMULARIO.Components[i] as TDBDateEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
               end;
    
    
               end;
    
            end ;
    
    
    
    
           if VALIDOU=true then
            begin
                      (FORMULARIO.Components[i] as TDBDateEdit).Color:= CorCertO;
            end;
           end;
    
    
    
    
       //////////////////////////////////////////////////////////////
    //TMaskEdit
    
    
    
           if (FORMULARIO.Components[i] is TMaskEdit) then
       begin
    
          indexOfItem := -1;
          ii:=0;
          for ii := Low(CAMPOSnoBANCO) to High(CAMPOSnoBANCO) do
          //LOCALIZANDO DENTRO da matriz
           if UpperCase(CAMPOSnoBANCO[ii]) = UpperCase((FORMULARIO.Components[i] as TMaskEdit).name ) then
             begin
             BREAK;
             end;
             VALIDOU:=True;
           if EhRequerido[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)]=true then
             begin
                 if((FORMULARIO.Components[i] as TMaskEdit).text = '')then
                 begin
                    if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TDBDateEdit' end;
                     MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TMaskEdit).Name)+' não pode ser NULO'+ #13#10+Trim(MSGaglutinada)   ;
                     (FORMULARIO.Components[i] as TMaskEdit).Color:=corERRO; VALIDOU:=false ;
                 end;
    
             end;
           if EhEMAIL[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)]=true  then
            begin
              if ValidEmail((FORMULARIO.Components[i] as TMaskEdit).text) = False  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TMaskEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TMaskEdit).Name)+' DEVE ser válido'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TMaskEdit).Color:=corERRO; VALIDOU:=false ;
               end;
            end ;
    
           if EhData[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)]=true  then
            begin
              if DataValida((FORMULARIO.Components[i] as TMaskEdit).text)=false  then
               begin
                     if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TMaskEdit' end;
                     MSGaglutinada :=   UpperCase((FORMULARIO.Components[i] as TMaskEdit).Name)+' DEVE uma data Válida'+ #13#10+Trim(MSGaglutinada)  ;
                     (FORMULARIO.Components[i] as TMaskEdit).Color:=corERRO; VALIDOU:=false ;
               end
               else
              begin
              if (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)])<=StrToDate((FORMULARIO.Components[i] as TMaskEdit).text)then
                   begin
    
                         if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TMaskEdit' end;
                          MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TMaskEdit).Name)+' não pode maior ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                          (FORMULARIO.Components[i] as TMaskEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
              end;
              if (OperadorDataMaior[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)] <> '') then
              begin
                  if StrToDate(OperadorDatamaior[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)])>=StrToDate((FORMULARIO.Components[i] as TMaskEdit).text)then
                   begin
    
                           if PrimeiroCampo<0 then begin PrimeiroCampo:=i;TIPO:='TMaskEdit' end;
                            MSGaglutinada :=  UpperCase((FORMULARIO.Components[i] as TMaskEdit).Name)+' não pode menor ou igual à '+ (OperadorDatamenor[ansiindexstr((FORMULARIO.Components[i] as TMaskEdit).name , CAMPOSnoBANCO)])+#13#10+Trim(MSGaglutinada)   ;
                            (FORMULARIO.Components[i] as TMaskEdit).Color:=corERRO; VALIDOU:=false ;
    
                   end;
               end;
    
    
               end;
    
            end ;
    
    
    
    
           if VALIDOU=true then
            begin
                      (FORMULARIO.Components[i] as TMaskEdit).Color:= CorCertO;
            end;
           end;
    
    
        end;
    
    
    
    ///////////////////////////////////////////////////////////
    
    if MSGaglutinada <>'' then
            begin
            MSGaglutinada:=ReplaceAll(MSGaglutinada,CAMPOSnoBANCO,CAMPOSexibirCOMO,True) ;
            MessageDlg('O(s) campo(s): '+#13#10+MSGaglutinada , mtWarning, [mbok],0 )   ;
            if TIPO='TDBEdit' then
             begin
                (FORMULARIO.Components[PrimeiroCampo] as TDBEdit).SetFocus;
             end
             else if TIPO='TDBLookupComboBox' then
             begin
                (FORMULARIO.Components[PrimeiroCampo] as TDBLookupComboBox).SetFocus
             end
               else if TIPO='TMaskEdit' then begin
                  (FORMULARIO.Components[PrimeiroCampo] as TMaskEdit).SetFocus
             end
             else if TIPO='TDBDateEdit' then begin
                  (FORMULARIO.Components[PrimeiroCampo] as TDBDateEdit).SetFocus
             end
             else if TIPO='TDateEdit' then begin
                  (FORMULARIO.Components[PrimeiroCampo] as TDateEdit).SetFocus
             end;
    
    
    
             Abort;
            end;
    
    
    
     result := true;
    
    end;
    
    
    
    function ReplaceAll(const Subject: String;
      const OldPatterns, NewPatterns: array of String;
      IgnoreCase: Boolean): String;
    var
      ReplaceFlags: TReplaceFlags;
      NewPattern: String;
      I: Integer;
    begin
      ReplaceFlags := [rfReplaceAll];
      if IgnoreCase then
        Include(ReplaceFlags, rfIgnoreCase);
      Result := Subject;
      for I := Low(OldPatterns) to High(OldPatterns) do
      begin
        if I <= High(NewPatterns) then
          NewPattern := NewPatterns[I]
        else
          NewPattern := '';
        Result := StringReplace(Result, OldPatterns[I], NewPattern, ReplaceFlags);
      end;
    end;
    
    
    
      function ValidEmail(email: string): boolean;
    
    
     var
      RegEx: TRegEx;
    begin
      RegEx := TRegex.Create('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]*[a-zA-Z0-9]+$');
      Result := RegEx.Match(email).Success;
     end;
    
    
    function DataValida(StrD: string): Boolean;
    var
    DataDigitadaNoEdit:TDateTime;
    
    
    
      {Testa se uma data é valida}
      begin
        Result := true;
        try
        //ShowMessage((StrD));
        if (StrD<>'  /  /  ') and (StrD <> '  /  /    ') and (StrD <> null)  and (StrD<>'')then
         begin
             TryStrToDate(StrD,DataDigitadaNoEdit)  ;
    
         end
         else
         Begin
              Result:=False;
             // Abort;
         end;
    
    
        except
          on EConvertError do Result:=False;
        end;
      end;
       function NumeroValido(StrD: string): Boolean;
      {Testa se uma data é valida}
      begin
        Result := true;
        try
        StrToDate(StrD);
        except
          on EConvertError do Result:=False;
        end;
      end;
4

1 回答 1

1

可以在运行时将类类型存储在变量中。但是,您不能使用该变量执行类型转换。强制转换是在编译时评估的,并且必须使用具体类型来强制转换。

此外,在这种情况下,您根本不需要那种类型转换。对于初学者,您的大多数as演员都在执行您已经使用is. 但更重要的是,Name它是一个公共属性,TComponent因此您根本不需要类型转换来访问它,例如:

if SameText(CAMPOSnoBANCO[ii], FORMULARIO.Components[i].Name) then

对于其他未公开/未发布但在基类中受保护Text的属性,例如和Color是,您可以使用访问器类来访问它们,或者您可以使用 RTTI 代替。

显示的代码是不必要的重复和冗长,其中有很多可以简化的东西。

尝试更多类似的东西:

type
  TControlAccess = class(TControl)
  end;

function ValidaCampoObrigatorio(): Boolean;
var
  i, ii: Integer;
  MSGaglutinada: String;
  VALIDOU: Boolean;
  Comp: TComponent;
  Ctrl, PrimeiroCampo: TWinControl;

  function GetControlText(ACtrl: TControl): String;
  begin
    Result := TControlAccess(ACtrl).Text;
    // alternatively:
    // Result := TypInfo.GetStrProp(ACtrl, 'Text');
  end;

  procedure SetControlColor(ACtrl: TControl; AValue: TColor);
  begin
    TControlAccess(ACtrl).Color := AValue;
    // alternatively:
    // TypInfo.SetOrdProp(ACtrl, 'Color', AValue);
  end;

begin
  CorERRO := RGB(255, 218, 185);
  CorCertO := 12582911;//$FF000005;

  Result := False;

  MSGaglutinada := '';
  PrimeiroCampo := nil;
    
  for i := 0 to FORMULARIO.ComponentCount - 1 do
  begin
    Comp := FORMULARIO.Components[i];
    if not (Comp is TWinControl) then Continue;
    Ctrl := TWinControl(Comp);

    //LOCALIZANDO DENTRO da matriz
    ii := AnsiIndexText(Ctrl.Name, CAMPOSnoBANCO);

    VALIDOU := True;

    if EhRequerido[ii] then
    begin
      if (GetControlText(Ctrl) = '') then
      begin
        MSGaglutinada := UpperCase(Ctrl.Name) + ' não pode ser NULO' + #13#10 + Trim(MSGaglutinada);
        VALIDOU := False;
      end;
    end;

    if EhEMAIL[ii] then
    begin
      if not ValidEmail(GetControlText(Ctrl)) then
      begin
        MSGaglutinada := UpperCase(Ctrl.Name) + ' DEVE ser válido' + #13#10 + Trim(MSGaglutinada);
        VALIDOU := False;
      end;
    end;

    if EhData[ii] then
    begin
      if not DataValida(GetControlText(Ctrl)) then
      begin
        MSGaglutinada := UpperCase(Ctrl.Name) + ' DEVE uma data Válida' + #13#10 + Trim(MSGaglutinada);
        VALIDOU := False;
      end
      else
      begin
        if (OperadorDatamenor[ii] <> '') then
        begin
          if StrToDate(OperadorDatamenor[ii]) <= StrToDate(GetControlText(Ctrl)) then
          begin
            MSGaglutinada := UpperCase(Ctrl.Name) + ' não pode maior ou igual à ' + OperadorDatamenor[ii] + #13#10 + Trim(MSGaglutinada);
            VALIDOU := False;
          end;
        end;
        if (OperadorDataMaior[ii] <> '') then
        begin
          if StrToDate(OperadorDatamaior[ii]) >= StrToDate(GetControlText(Ctrl)) then
          begin
            MSGaglutinada := UpperCase(Ctrl.Name) + ' não pode menor ou igual à ' + OperadorDatamenor[ii] + #13#10 + Trim(MSGaglutinada);
            VALIDOU := False;
          end;
        end;
      end;
    end;

    if not VALIDOU then
    begin
      if PrimeiroCampo = nil then PrimeiroCampo := Ctrl;
      SetControlColor(Ctrl, corERRO);
    end
    else
      SetControlColor(Ctrl, CorCertO);
  end;

  ///////////////////////////////////////////////////////////
    
  if MSGaglutinada <> '' then
  begin
    MSGaglutinada := ReplaceAll(MSGaglutinada, CAMPOSnoBANCO, CAMPOSexibirCOMO, True);
    MessageDlg('O(s) campo(s): ' + #13#10 + MSGaglutinada, mtWarning, [mbok], 0);
    PrimeiroCampo.SetFocus;
    Abort;
  end;

  Result := True;    
end;
于 2021-03-10T22:11:31.430 回答