Вы находитесь на странице: 1из 2

// Configura TODOS os campos de TODOS os ClientDataSets, isso , // atribui para cada campo do CDS uma funo de validao no // evento

OnSetText. Isso quer dizer que quando o usurio entrar // com valores em campos inteiros, datas e decimais, ele ser // validado. Com isso, caso adicionamos mais 5 CDS's no DM, // no teremos que nos preocupar mais com isso. procedure TDMCadastros.configuraTodosOsCampos; var I, J: integer; begin for I := 0 to Self.ComponentCount -1 do begin if Self.Components[I] is TClientDataSet then begin for J := 0 to TClientDataSet(Self.Components[I]).FieldCount -1 do begin if TClientDataSet(Self.Components[I]).Fields[J] is TDateField then begin TDateField(TClientDataSet(Self.Components[I]).Fields[J]).OnSetText := Self.setTextDateField; end else if TClientDataSet(Self.Components[I]).Fields[J] is TIntegerField th en begin TIntegerField(TClientDataSet(Self.Components[I]).Fields[J]).OnSetText := Self.setTextIntegerField; end else if TClientDataSet(Self.Components[I]).Fields[J] is TFMTBCDField the n begin TFMTBCDField(TClientDataSet(Self.Components[I]).Fields[J]).OnSetText : = Self.setTextDecimalField; end else if TClientDataSet(Self.Components[i]).Fields[J] is TFloatField then begin TFloatField(TClientDataSet(Self.Components[i]).Fields[J]).OnSetText := Self.setTextDecimalField; end; end; end; end; end;

// Funes que so atribuidas ao evento OnSetText de cada campo: procedure TDMCadastros.setTextDateField(Sender: TField; const Text: string); begin try if Text = ' / / ' then Sender.AsString := '' else Sender.AsString := Text; except on EConvertError do Application.MessageBox('Por favor, insira uma data vlida.', 'Data invlida', MB_OK + MB_ICONINFORMATION); end; end;

procedure TDMCadastros.setTextDecimalField(Sender: TField; const Text: string); begin try if Copy(Text, 0, 1) = '-' then Application.MessageBox('Por favor, insira valores vlidos em campos decimais .', 'Valores invlidos em campos decimais', MB_OK + MB_ICONINFORMATION) else if Text = '' then Sender.AsFloat := 0.00 else Sender.AsFloat := StrToFloat(Text); except on EConvertError do Application.MessageBox('Por favor, insira valores vlidos em campos decimais .', 'Valores invlidos em campos decimais', MB_OK + MB_ICONINFORMATION); on Exception do Application.MessageBox('Esse nmero decimal muito grande.', 'Nmero fora dos l imites', MB_OK + MB_ICONINFORMATION); end; end; procedure TDMCadastros.setTextIntegerField(Sender: TField; const Text: string); begin try if Copy(Text, 0, 1) = '-' then Application.MessageBox('Por favor, insira valores vlidos em campos numricos. ', 'Valores invlidos para campos numricos', MB_OK + MB_ICONINFORMATION) else if Text = '' then Sender.AsInteger := 0 else Sender.AsInteger := StrToInt(Text); except on EConvertError do Application.MessageBox('Por favor, insira valores vlidos em campos numricos. ', 'Valores invlidos para campos numricos', MB_OK + MB_ICONINFORMATION); end; end;

Вам также может понравиться