Advertisement
miguelhosttimer

cadastro pessoas usando ini

May 22nd, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.63 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IniFiles;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Edit1: TEdit;
  13.     Edit2: TEdit;
  14.     Edit3: TEdit;
  15.     Edit4: TEdit;
  16.     ComboBox1: TComboBox;
  17.     Label6: TLabel;
  18.     Edit6: TEdit;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure Edit6Change(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.     function CPFExists(const CPF: string): Boolean;
  25.   public
  26.     { Public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.dfm}
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. var
  38.   IniFile: TIniFile;
  39.   FilePath: string;
  40. begin
  41.   FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  42.   if not FileExists(FilePath) then
  43.   begin
  44.     IniFile := TIniFile.Create(FilePath);
  45.     try
  46.       IniFile.WriteInteger('Pessoa', 'Count', 0);
  47.     finally
  48.       IniFile.Free;
  49.     end;
  50.   end;
  51. end;
  52.  
  53. procedure TForm1.Button1Click(Sender: TObject);
  54. var
  55.   IniFile: TIniFile;
  56.   FilePath: string;
  57.   Count: Integer;
  58. begin
  59.   if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '') and (ComboBox1.Text <> '') then
  60.   begin
  61.     if CPFExists(Edit2.Text) then
  62.     begin
  63.       ShowMessage('Já existe um cadastro com o mesmo CPF.');
  64.       Edit1.Text := '';
  65.       Edit2.Text := '';
  66.       Edit3.Text := '';
  67.       Edit4.Text := '';
  68.       ComboBox1.Text := '';
  69.     end
  70.     else
  71.     begin
  72.       FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  73.       IniFile := TIniFile.Create(FilePath);
  74.       try
  75.         Count := IniFile.ReadInteger('Pessoa', 'Count', 0) + 1;
  76.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'Nome', Edit1.Text);
  77.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'CPF', Edit2.Text);
  78.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'Placa', Edit3.Text);
  79.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'QuemLiberou', Edit4.Text);
  80.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'OndeFoi', ComboBox1.Text);
  81.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'HoraEntrada', TimeToStr(Now));
  82.         IniFile.WriteInteger('Pessoa', 'Count', Count);
  83.       finally
  84.         IniFile.Free;
  85.       end;
  86.  
  87.       // Limpa os campos de edição
  88.       Edit1.Text := '';
  89.       Edit2.Text := '';
  90.       Edit3.Text := '';
  91.       Edit4.Text := '';
  92.       ComboBox1.Text := '';
  93.     end;
  94.   end
  95.   else
  96.   begin
  97.     ShowMessage('Por favor, preencha todos os campos antes de salvar.');
  98.   end;
  99. end;
  100.  
  101. procedure TForm1.Edit6Change(Sender: TObject);
  102. var
  103.   IniFile: TIniFile;
  104.   FilePath: string;
  105.   CPF: string;
  106.   Count, I: Integer;
  107. begin
  108.   if Length(Edit6.Text) = 3 then
  109.   begin
  110.     FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  111.     if FileExists(FilePath) then
  112.     begin
  113.       IniFile := TIniFile.Create(FilePath);
  114.       try
  115.         Count := IniFile.ReadInteger('Pessoa', 'Count', 0);
  116.         for I := 1 to Count do
  117.         begin
  118.           CPF := IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', '');
  119.           if Pos(Edit6.Text, Copy(CPF, 1, 3)) > 0 then
  120.           begin
  121.             // Preenche os campos de edição com os dados da pessoa
  122.             Edit1.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'Nome', '');
  123.             Edit2.Text := CPF;
  124.             Edit3.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'Placa', '');
  125.             Edit4.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'QuemLiberou', '');
  126.             ComboBox1.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'OndeFoi', '');
  127.             Break;
  128.           end;
  129.         end;
  130.       finally
  131.         IniFile.Free;
  132.       end;
  133.     end;
  134.   end;
  135. end;
  136.  
  137.  
  138.  
  139. function TForm1.CPFExists(const CPF: string): Boolean;
  140. var
  141.   IniFile: TIniFile;
  142.   FilePath: string;
  143.   Count, I: Integer;
  144. begin
  145.   Result := False;
  146.   FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  147.   if FileExists(FilePath) then
  148.   begin
  149.     IniFile := TIniFile.Create(FilePath);
  150.     try
  151.       Count := IniFile.ReadInteger('Pessoa', 'Count', 0);
  152.       for I := 1 to Count do
  153.       begin
  154.         if IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', '') = CPF then
  155.         begin
  156.           Result := True;
  157.           Break;
  158.         end;
  159.       end;
  160.     finally
  161.       IniFile.Free;
  162.     end;
  163.   end;
  164. end;
  165.  
  166. end.
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement