quinta-feira, 15 de outubro de 2009

Limpar os campos do formulário de uma só vez.

Abaixo, segue um procedimento para limpar os campos do formulário. Neste exemplo, foram colocados os campos edits e memo. Podem ser colocados outros tipos de componente.

-------------------------------------------------------------------------------
procedure Form1.LimpaCampos;
var i: Integer;

begin

for i := 0 to ComponentCount - 1 do
begin
// Limpa os componentes do tipo Edit.
if (Components[i] is TEdit) then
(Components[i] as TEdit).Clear;
// Limpa os componentes do tipo Memo.
else if (Components[i] is TMemo) then
(Components[i] as TMemo).Clear;
end;
end;
-------------------------------------------------------------------------------

quinta-feira, 1 de outubro de 2009

Pegar somente os números de um texto...

Para pegar somente os números de uma string, pode usar essa função. Ela percorre toda a string e adiciona à variavel vText, os números. O resultado da função é uma string com todos números encontrados.

//-------------------------------------------------------------------------
//
Início

Function TForm1.RetornaSomenteOsNumeros(vsTexto: String): String;
var
a: Integer;
vNum, vText: string;
begin
vText := '';
for a := 1 to length(vsTexto) do
begin
vNum := Copy(vsTexto, a, 1);
if (vNum = '1') or (vNum = '2') or (vNum = '3') or (vNum = '4') or (vNum = '5') or(vNum = '6') or (vNum = '7') or (vNum = '8') or (vNum = '9') or (vNum = '0') then
vtext := vtext + vNum;
end;
result := vText;
end;

// Fim

//-------------------------------------------------------------------------

Validação de Email

Segue abaixo, a rotina de validação de email pelo delphi 7.

// Rotina de validação de email: Recebe um texto e devolve um boolean:
function Tform1.ValidaEmail(email: String): Boolean;
const
atom_chars = [#33..#255] - ['(', ')', '<', '>', '@', ',', ';', ':',
'\', '/', '"', '.', '[', ']', #127];

quoted_string_chars = [#0..#255] - ['"', #13, '\'];
letters = ['A'..'Z', 'a'..'z'];
letters_digits = ['0'..'9', 'A'..'Z', 'a'..'z'];
subdomain_chars = ['-', '0'..'9', 'A'..'Z', 'a'..'z'];

type
States = (STATE_BEGIN, STATE_ATOM, STATE_QTEXT, STATE_QCHAR,
STATE_QUOTE, STATE_LOCAL_PERIOD, STATE_EXPECTING_SUBDOMAIN,
STATE_SUBDOMAIN, STATE_HYPHEN);
var
State: States;
i, n, subdomains: integer;
c: char;
begin
State := STATE_BEGIN;
n := Length(email);
i := 1;

subdomains := 1;

while (i <= n) do
begin
c := email[i];
case State of
STATE_BEGIN:
if c in atom_chars then
State := STATE_ATOM
else if c = '"' then
State := STATE_QTEXT
else
break;
STATE_ATOM:
if c = '@' then
State := STATE_EXPECTING_SUBDOMAIN
else
if c = '.' then
State := STATE_LOCAL_PERIOD
else
if not (c in atom_chars) then
break;
STATE_QTEXT:
if c = '\' then
State := STATE_QCHAR
else if c = '"' then
State := STATE_QUOTE
else if not (c in quoted_string_chars) then
break;
STATE_QCHAR:
State := STATE_QTEXT;
STATE_QUOTE:
if c = '@' then
State := STATE_EXPECTING_SUBDOMAIN
else if c = '.' then
State := STATE_LOCAL_PERIOD
else
break;
STATE_LOCAL_PERIOD:
if c in atom_chars then
State := STATE_ATOM
else if c = '"' then
State := STATE_QTEXT
else
break;
STATE_EXPECTING_SUBDOMAIN:
if c in letters then
State := STATE_SUBDOMAIN
else
break;
STATE_SUBDOMAIN:
if c = '.' then
begin
inc(subdomains);
State := STATE_EXPECTING_SUBDOMAIN
end
else
if c = '-' then
State := STATE_HYPHEN
else
if not (c in letters_digits) then
break;
STATE_HYPHEN:
if c in letters_digits then
State := STATE_SUBDOMAIN
else
if c <> '-' then
break;
end;
inc(i);
end;
if i <= n then
Result := False
else
Result := (State = STATE_SUBDOMAIN) and (subdomains >= 2);
end;


// Para chamar a rotina, tem que passar o email como parâmetro.
// Exemplo de utilização:
// Coloque um Edit e um botão em um form. O Edit deverá chamar edEmail.
// No evento OnClick do botão, coloque o código:

if Trim(edEmail.Text) <> '' then
begin
if not ValidaEmail(Trim(edEmail.Text)) then
ShowMessage('Email informado INVÁLIDO')
else
ShowMessage('Email informado é VÁLIDO');
end;


Ao acionar o botão, será verificado se o email é válido ou não.

Até a próxima!

Aprendendo Delphi e C#

Olá!

Logo começarei a postar dicas de programação em Delphi e C#....

São coisas simples, mas que no dia a dia, ajudam... e muito!

Abraços