Página 1 de 1

Sistema de Cadastro com Arquivo em Delphi

Enviado: quinta mar 06, 2025 2:26 am
por RogerBok
Vamos criar agora uma versão ainda mais completa do mini sistema de cadastro em Delphi, incluindo:

Adicionar pessoas (nome e idade)

Pesquisar por nome

Listar todos os cadastros

Salvar e carregar os dados de um arquivo


unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
TForm1 = class(TForm)
EditNome: TEdit;
EditIdade: TEdit;
EditPesquisar: TEdit;
ButtonAdicionar: TButton;
ButtonPesquisar: TButton;
ButtonSalvar: TButton;
ButtonCarregar: TButton;
MemoLista: TMemo;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure ButtonAdicionarClick(Sender: TObject);
procedure ButtonPesquisarClick(Sender: TObject);
procedure ButtonSalvarClick(Sender: TObject);
procedure ButtonCarregarClick(Sender: TObject);
private
procedure AtualizarLista;
public
Nomes: array of string;
Idades: array of Integer;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonAdicionarClick(Sender: TObject);
begin
if EditNome.Text = '' then
ShowMessage('Digite um nome válido')
else
begin
SetLength(Nomes, Length(Nomes) + 1);
SetLength(Idades, Length(Idades) + 1);

Nomes[High(Nomes)] := EditNome.Text;
Idades[High(Idades)] := StrToInt(EditIdade.Text);

EditNome.Clear;
EditIdade.Clear;
EditNome.SetFocus;

AtualizarLista;
end;
end;

procedure TForm1.ButtonPesquisarClick(Sender: TObject);
var
i: Integer;
Resultado: TStringList;
begin
Resultado := TStringList.Create;
try
for i := 0 to High(Nomes) do
if Pos(LowerCase(EditPesquisar.Text), LowerCase(Nomes)) > 0 then
Resultado.Add('Nome: ' + Nomes + ' | Idade: ' + IntToStr(Idades));

MemoLista.Lines := Resultado;
finally
Resultado.Free;
end;
end;

procedure TForm1.ButtonSalvarClick(Sender: TObject);
var
i: Integer;
Arquivo: TextFile;
begin
AssignFile(Arquivo, 'cadastros.txt');
Rewrite(Arquivo);
for i := 0 to High(Nomes) do
Writeln(Arquivo, Nomes + ';' + IntToStr(Idades));
CloseFile(Arquivo);
ShowMessage('Dados salvos com sucesso!');
end;

procedure TForm1.ButtonCarregarClick(Sender: TObject);
var
Arquivo: TextFile;
Linha: string;
Parts: TArray<string>;
begin
if not FileExists('cadastros.txt') then
begin
ShowMessage('Arquivo não encontrado!');
Exit;
end;

AssignFile(Arquivo, 'cadastros.txt');
Reset(Arquivo);

SetLength(Nomes, 0);
SetLength(Idades, 0);

while not Eof(Arquivo) do
begin
Readln(Arquivo, Linha);
Parts := Linha.Split([';']);
SetLength(Nomes, Length(Nomes) + 1);
SetLength(Idades, Length(Idades) + 1);
Nomes[High(Nomes)] := Parts[0];
Idades[High(Idades)] := StrToInt(Parts[1]);
end;

CloseFile(Arquivo);
AtualizarLista;
ShowMessage('Dados carregados com sucesso!');
end;

procedure TForm1.AtualizarLista;
var
i: Integer;
begin
MemoLista.Clear;
for i := 0 to High(Nomes) do
MemoLista.Lines.Add('Nome: ' + Nomes + ' | Idade: ' + IntToStr(Idades));
end;

end.



Como montar o formulário

EditNome e Label1 → Nome:

EditIdade e Label2 → Idade:

EditPesquisar e Label3 → Pesquisar:

ButtonAdicionar → Adicionar

ButtonPesquisar → Pesquisar

ButtonSalvar → Salvar no arquivo

ButtonCarregar → Carregar do arquivo

MemoLista → para mostrar todos os cadastros



Como funciona

Adicione pessoas com nome e idade.

Clique Adicionar para incluir no cadastro interno e exibir no Memo.

Clique Pesquisar para filtrar por nomes que contenham o termo digitado.

Clique Salvar no arquivo para gravar todos os cadastros em cadastros.txt.

Clique Carregar do arquivo para recuperar os cadastros salvos anteriormente.

✅ Esse sistema agora é um mini software real, totalmente funcional, que pode ser expandido com:

Ordenação por nome ou idade

Remoção de cadastros

Interface mais moderna com TListView ou TStringGrid

Validações adicionais (idade negativa, campos vazios etc.)


==============================================================

https://www.nasciweb.com.br