Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IniFiles;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Edit1: TEdit;
- Edit2: TEdit;
- Edit3: TEdit;
- Edit4: TEdit;
- ComboBox1: TComboBox;
- Label6: TLabel;
- Edit6: TEdit;
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure Edit6Change(Sender: TObject);
- private
- { Private declarations }
- function CPFExists(const CPF: string): Boolean;
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- var
- IniFile: TIniFile;
- FilePath: string;
- begin
- FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
- if not FileExists(FilePath) then
- begin
- IniFile := TIniFile.Create(FilePath);
- try
- IniFile.WriteInteger('Pessoa', 'Count', 0);
- finally
- IniFile.Free;
- end;
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- IniFile: TIniFile;
- FilePath: string;
- Count: Integer;
- begin
- if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '') and (ComboBox1.Text <> '') then
- begin
- if CPFExists(Edit2.Text) then
- begin
- ShowMessage('Já existe um cadastro com o mesmo CPF.');
- Edit1.Text := '';
- Edit2.Text := '';
- Edit3.Text := '';
- Edit4.Text := '';
- ComboBox1.Text := '';
- end
- else
- begin
- FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
- IniFile := TIniFile.Create(FilePath);
- try
- Count := IniFile.ReadInteger('Pessoa', 'Count', 0) + 1;
- IniFile.WriteString('Pessoa' + IntToStr(Count), 'Nome', Edit1.Text);
- IniFile.WriteString('Pessoa' + IntToStr(Count), 'CPF', Edit2.Text);
- IniFile.WriteString('Pessoa' + IntToStr(Count), 'Placa', Edit3.Text);
- IniFile.WriteString('Pessoa' + IntToStr(Count), 'QuemLiberou', Edit4.Text);
- IniFile.WriteString('Pessoa' + IntToStr(Count), 'OndeFoi', ComboBox1.Text);
- IniFile.WriteString('Pessoa' + IntToStr(Count), 'HoraEntrada', TimeToStr(Now));
- IniFile.WriteInteger('Pessoa', 'Count', Count);
- finally
- IniFile.Free;
- end;
- // Limpa os campos de edição
- Edit1.Text := '';
- Edit2.Text := '';
- Edit3.Text := '';
- Edit4.Text := '';
- ComboBox1.Text := '';
- end;
- end
- else
- begin
- ShowMessage('Por favor, preencha todos os campos antes de salvar.');
- end;
- end;
- procedure TForm1.Edit6Change(Sender: TObject);
- var
- IniFile: TIniFile;
- FilePath: string;
- CPF: string;
- Count, I: Integer;
- begin
- if Length(Edit6.Text) = 3 then
- begin
- FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
- if FileExists(FilePath) then
- begin
- IniFile := TIniFile.Create(FilePath);
- try
- Count := IniFile.ReadInteger('Pessoa', 'Count', 0);
- for I := 1 to Count do
- begin
- CPF := IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', '');
- if Pos(Edit6.Text, Copy(CPF, 1, 3)) > 0 then
- begin
- // Preenche os campos de edição com os dados da pessoa
- Edit1.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'Nome', '');
- Edit2.Text := CPF;
- Edit3.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'Placa', '');
- Edit4.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'QuemLiberou', '');
- ComboBox1.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'OndeFoi', '');
- Break;
- end;
- end;
- finally
- IniFile.Free;
- end;
- end;
- end;
- end;
- function TForm1.CPFExists(const CPF: string): Boolean;
- var
- IniFile: TIniFile;
- FilePath: string;
- Count, I: Integer;
- begin
- Result := False;
- FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
- if FileExists(FilePath) then
- begin
- IniFile := TIniFile.Create(FilePath);
- try
- Count := IniFile.ReadInteger('Pessoa', 'Count', 0);
- for I := 1 to Count do
- begin
- if IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', '') = CPF then
- begin
- Result := True;
- Break;
- end;
- end;
- finally
- IniFile.Free;
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement