Métodos e funções para uso em equipe ou mais de um projeto


Abrir formulário
procedure AbreForm(aClasseForm: TComponentClass; aForm: TForm);
begin
  Application.CreateForm(aClasseForm, aForm);
  try
    aForm.ShowModal;
  finally
    aForm.Free;
  end;
end;
Utilização:
AbreForm(TForm1, Form1);

Remove os espaços em branco à direita da string
function RTrim(Texto: string): string;
var
  i: integer;
begin
  i := Length(Texto)+1;
  while true do begin
    Dec(i);
    if i <= 0 then
      break;
    if Texto[i] <> #32 then
      break;
  end;
  Result := Copy(Texto, 1, i);
end;
Utilização:
Edit1.Text := RTrim(Edit1.Text);

Remove os espaços em branco à direita da string
function LTrim(Texto: string): string;

var

  i: integer;

begin

  i := 0;

  while true do begin

    inc(i);

    if i > Length(Texto) then

      break;

    if Texto[i] <> #32 then

      break;

  end;

  Result := Copy(Texto, i, Length(Texto));

end;
Utilização:
Edit2.Text := LTrim(Edit2.Text);

Valida uma data
function DateValidate(const aData: string): boolean;
begin
  try
    StrToDate(aData);
    Result := true;
  except
    Result := false;
  end;
end;
Utilização:
if not DateValidate(Edit3.Text) then
  ShowMessage('Data inválida!');

Remove carácter passado como parâmetro
function RemoveChar (const Ch: Char; const S: string): string;
var
  Posicao: integer;
begin
  Result := S;
  Posicao := Pos(Ch, Result);
  while Posicao > 0 do begin
    Delete(Result, Posicao, 1);
    Posicao := Pos(Ch, Result);
  end;
end;
Utilização:
Edit4.Text := RemoveChar('@', Edit4.Text);

Remove acentos da string passada como parâmetro
function RemoveAcento(Str: string): string;
const
  ComAcento = 'àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ';
  SemAcento = 'aaeouaoaeioucuAAEOUAOAEIOUCU';
var
  x: integer;
begin
  for x := 1 to Length(Str) do
    if Pos(Str[x], ComAcento) <> 0 then
      Str[x] := SemAcento[Pos(Str[x],ComAcento)];
  Result := Str;
end;
Utilização:
Edit5.Text := RemoveAcento(Edit5.Text);

Pesquisa um caractere na string, retornando se achou
function BuscaTexto (Busca, Text: string): boolean;
begin
  Result := (Pos(Busca, Text) > 0);
end;
Utilização:
if BuscaTexto('luc', Edit6.Text) then
  ShowMessage('Texto encontrado!');

Gera caracteres aleatórios para senha
function GeraSenha (aQuant: integer): string;
var
  i: integer;
const
  str = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
begin
  for i:= 1 to aQuant do begin
    Randomize;
    Result := Result + str[Random(Length(str))+1];
  end;
end;
Utilização:
Edit7.Text := GeraSenha(6);

Retorna o valor do Generator onde você pode incrementar o valor do mesmo
function GeneratorID (aName: string; Connection: TSQLConnection; aInc: Boolean): integer;
var
  Qry: TSQLQuery;
begin
  Qry := TSQLQuery.Create(nil);
  try
    Qry.SQLConnection := Connection;
    if aInc then
      Qry.SQL.Add('SELECT GEN_ID('+aName+', 1) FROM RDB$DATABASE')
    else
      Qry.SQL.Add('SELECT GEN_ID('+aName+', 0) FROM RDB$DATABASE');
    Qry.Open;
    Result := Qry.Fields[0].AsInteger;
  finally
    FreeAndNil(Qry);
  end;
end;

0 comentários:

Postar um comentário