Advertisement
miguelhosttimer

cadastro usando banco .ini delphi

May 22nd, 2024
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.83 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.     Edit5: TEdit;
  18.     Label6: TLabel;
  19.     Memo1: TMemo;
  20.     Edit6: TEdit;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Edit5Change(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure Edit6Change(Sender: TObject);
  25.   private
  26.     { Private declarations }
  27.     function CPFExists(const CPF: string): Boolean;
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.dfm}
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. var
  41.   IniFile: TIniFile;
  42.   FilePath: string;
  43. begin
  44.   FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  45.   if not FileExists(FilePath) then
  46.   begin
  47.     IniFile := TIniFile.Create(FilePath);
  48.     try
  49.       IniFile.WriteInteger('Pessoa', 'Count', 0);
  50.     finally
  51.       IniFile.Free;
  52.     end;
  53.   end;
  54. end;
  55.  
  56. procedure TForm1.Button1Click(Sender: TObject);
  57. var
  58.   IniFile: TIniFile;
  59.   FilePath: string;
  60.   Count: Integer;
  61. begin
  62.   if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '') and (ComboBox1.Text <> '') then
  63.   begin
  64.     if CPFExists(Edit2.Text) then
  65.     begin
  66.       ShowMessage('Já existe um cadastro com o mesmo CPF.');
  67.       Edit1.Text := '';
  68.       Edit2.Text := '';
  69.       Edit3.Text := '';
  70.       Edit4.Text := '';
  71.       ComboBox1.Text := '';
  72.     end
  73.     else
  74.     begin
  75.       FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  76.       IniFile := TIniFile.Create(FilePath);
  77.       try
  78.         Count := IniFile.ReadInteger('Pessoa', 'Count', 0) + 1;
  79.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'Nome', Edit1.Text);
  80.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'CPF', Edit2.Text);
  81.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'Placa', Edit3.Text);
  82.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'QuemLiberou', Edit4.Text);
  83.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'OndeFoi', ComboBox1.Text);
  84.         IniFile.WriteString('Pessoa' + IntToStr(Count), 'HoraEntrada', TimeToStr(Now));
  85.         IniFile.WriteInteger('Pessoa', 'Count', Count);
  86.       finally
  87.         IniFile.Free;
  88.       end;
  89.  
  90.       // Limpa os campos de edição
  91.       Edit1.Text := '';
  92.       Edit2.Text := '';
  93.       Edit3.Text := '';
  94.       Edit4.Text := '';
  95.       ComboBox1.Text := '';
  96.     end;
  97.   end
  98.   else
  99.   begin
  100.     ShowMessage('Por favor, preencha todos os campos antes de salvar.');
  101.   end;
  102. end;
  103.  
  104. procedure TForm1.Edit5Change(Sender: TObject);
  105. var
  106.   IniFile: TIniFile;
  107.   FilePath: string;
  108.   Nome: string;
  109.   Count, I: Integer;
  110. begin
  111.   FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  112.   if FileExists(FilePath) then
  113.   begin
  114.     IniFile := TIniFile.Create(FilePath);
  115.     try
  116.       Count := IniFile.ReadInteger('Pessoa', 'Count', 0);
  117.       for I := 1 to Count do
  118.       begin
  119.         Nome := IniFile.ReadString('Pessoa' + IntToStr(I), 'Nome', '');
  120.         if Pos(Edit5.Text, Nome) > 0 then
  121.         begin
  122.           Memo1.Lines.Clear;
  123.           Memo1.Lines.Add('Nome: ' + Nome);
  124.           Memo1.Lines.Add('CPF: ' + IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', ''));
  125.           Memo1.Lines.Add('Placa: ' + IniFile.ReadString('Pessoa' + IntToStr(I), 'Placa', ''));
  126.           Memo1.Lines.Add('Quem Liberou: ' + IniFile.ReadString('Pessoa' + IntToStr(I), 'QuemLiberou', ''));
  127.           Memo1.Lines.Add('Onde Foi: ' + IniFile.ReadString('Pessoa' + IntToStr(I), 'OndeFoi', ''));
  128.           Memo1.Lines.Add('Hora de Entrada: ' + IniFile.ReadString('Pessoa' + IntToStr(I), 'HoraEntrada', ''));
  129.         end;
  130.       end;
  131.     finally
  132.       IniFile.Free;
  133.     end;
  134.   end;
  135. end;
  136.  
  137. procedure TForm1.Edit6Change(Sender: TObject);
  138. var
  139.   IniFile: TIniFile;
  140.   FilePath: string;
  141.   Count, I: Integer;
  142. begin
  143.   FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  144.   if FileExists(FilePath) then
  145.   begin
  146.     IniFile := TIniFile.Create(FilePath);
  147.     try
  148.       Count := IniFile.ReadInteger('Pessoa', 'Count', 0);
  149.       for I := 1 to Count do
  150.       begin
  151.         if IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', '') = Edit6.Text then
  152.         begin
  153.           // Preenche os campos de edição com os dados da pessoa
  154.           Edit1.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'Nome', '');
  155.           Edit2.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', '');
  156.           Edit3.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'Placa', '');
  157.           Edit4.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'QuemLiberou', '');
  158.           ComboBox1.Text := IniFile.ReadString('Pessoa' + IntToStr(I), 'OndeFoi', '');
  159.           Break;
  160.         end;
  161.       end;
  162.     finally
  163.       IniFile.Free;
  164.     end;
  165.   end;
  166. end;
  167.  
  168. function TForm1.CPFExists(const CPF: string): Boolean;
  169. var
  170.   IniFile: TIniFile;
  171.   FilePath: string;
  172.   Count, I: Integer;
  173. begin
  174.   Result := False;
  175.   FilePath := IncludeTrailingPathDelimiter(GetEnvironmentVariable('USERPROFILE')) + 'Documents\seu_arquivo.ini';
  176.   if FileExists(FilePath) then
  177.   begin
  178.     IniFile := TIniFile.Create(FilePath);
  179.     try
  180.       Count := IniFile.ReadInteger('Pessoa', 'Count', 0);
  181.       for I := 1 to Count do
  182.       begin
  183.         if IniFile.ReadString('Pessoa' + IntToStr(I), 'CPF', '') = CPF then
  184.         begin
  185.           Result := True;
  186.           Break;
  187.         end;
  188.       end;
  189.     finally
  190.       IniFile.Free;
  191.     end;
  192.   end;
  193. end;
  194.  
  195. end.
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement