Program Delphi untuk File TXT

        Kita dalam belajar sebuah program tidak mungkin begitu saja langsung bisa, ada tahap demi tahap yang harus kita tempuh, kalau belajar sekarang kita sudah mudah karna sudah tersedia segudang ilmu di internet..
hehehehe.. Kali ini saya akan membahas tugas integrasi sistem yang menyambungkan file TXT dengan Delphi.. nah File txt ini akan menjadi sebuah  tempat penyimpanan data melaluui delphi..

Ok. kita langsung saja ke program untuk delphi..

unit TGS_UIR_I;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
Panel2: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Panel3: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button2: TButton;
Button3: TButton;
StringGrid1: TStringGrid;
Panel9: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel8: TPanel;
Panel10: TPanel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Edit4Change(Sender: TObject);
procedure Edit3KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
F : TextFile;
baris : Integer;
myRect: TGridRect;
begin
if (Panel3.Visible = False) Then
begin
Form1.Height := 510;
Panel3.Visible := True;
Panel3.Enabled := True;
end;
baris := StringGrid1.RowCount;
If ((StringGrid1.Cells[0,0] <> '')) Then
begin
StringGrid1.RowCount := baris+1;
baris := baris+1;
end;
if (baris < 10) then
StringGrid1.Cells[0,baris-1] := ' ' + IntToStr(baris)
else
if ((baris >= 10) and (baris-1 < 99)) then
StringGrid1.Cells[0,baris-1] := ' ' + IntToStr(baris)
else
StringGrid1.Cells[0,baris-1] := ' ' + IntToStr(baris);
StringGrid1.Cells[1,baris-1] := UpperCase(Edit1.Text);
StringGrid1.Cells[2,baris-1] := UpperCase(Edit2.Text);
StringGrid1.Cells[3,baris-1] := Edit3.Text;
StringGrid1.Cells[4,baris-1] := Edit4.Text;
myRect.Left := 0;
myRect.Top := baris-1;
myRect.Right := 4;
myRect.Bottom := baris-1;
StringGrid1.Selection := myRect;
Edit1.Text := '';
Edit2.Text := '';
Edit3.Text := '';
Edit4.Text := '';
end;
procedure TForm1.FormCreate(Sender: TObject);
var
F : TextFile;
FileHandle : Integer;
HighOrderSize : ^DWORD;
LowOrderSize : DWORD;
baris : Integer;
data : String;
status : Boolean;
begin
Button1.Enabled := False;
Button2.Enabled := False;
Status := False;
If FileExists('data.ret') = True Then
begin
FileHandle := 0;
Try
FileHandle := FileOpen('data.ret',fmOpenRead);
If (FileHandle = -1) Then
begin
ShowMessage('Gagal membuka File');
Exit;
end;
HighOrderSize := Nil;
LowOrderSize := GetFileSize(FileHandle, HighOrderSize);
If (LowOrderSize = INVALID_FILE_SIZE) Then
ShowMessage('Gagal mendapatkan ukuran file')
Else
If (LowOrderSize > 0) Then
begin
Form1.Height := 510;
Panel3.Visible := True;
Panel3.Enabled := True;
Status := True;
end
Else
begin
Form1.Height := 270;
Panel3.Enabled := False;
Panel3.Visible := False;
end;
Finally
FileClose(FileHandle);
End;
if (Status = True) then
begin
AssignFile(F, 'data.ret');
Reset(F);
baris := 1;
while Not EOF(F) do
begin
StringGrid1.RowCount := baris;
if (baris < 10) then
StringGrid1.Cells[0,baris-1] := ' ' + IntToStr(baris)
else
if ((baris >= 10) and (baris < 99)) then
StringGrid1.Cells[0,baris-1] := ' ' + IntToStr(baris)
else
StringGrid1.Cells[0,baris-1] := ' ' + IntToStr(baris);
readln(F, data);
StringGrid1.Cells[1,baris-1] := data;
readln(F, data);
StringGrid1.Cells[2,baris-1] := data;
readln(F, data);
StringGrid1.Cells[3,baris-1] := data;
readln(F, data);
StringGrid1.Cells[4,baris-1] := data;
baris := baris + 1;
end;
CloseFile(F);
end;
end
else
begin
Form1.Height := 270;
Panel3.Enabled := False;
Panel3.Visible := False;
AssignFile(F, 'data.ret');
ReWrite(F);
CloseFile(F);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
F : TextFile;
i : Integer;
begin
If (StringGrid1.Cells[0,0] <> '') Then
begin
AssignFile(F, 'data.ret');
ReWrite(F);
for i := 1 to StringGrid1.RowCount do
begin
Writeln (F, StringGrid1.Cells[1,i-1]);
Writeln (F, StringGrid1.Cells[2,i-1]);
Writeln (F, StringGrid1.Cells[3,i-1]);
Writeln (F, StringGrid1.Cells[4,i-1]);
end;
CloseFile(F);
end;
Form1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text := '';
Edit2.Text := '';
Edit3.Text := '';
Edit4.Text := '';
Edit1.SetFocus;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
If ((Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '')) Then
Button1.Enabled := True
Else
Button1.Enabled := False;
If ((Edit1.Text <> '') or (Edit2.Text <> '') or (Edit3.Text <> '') or (Edit4.Text <> '')) Then
Button2.Enabled := True
Else
Button2.Enabled := False;
end;
procedure TForm1.Edit4Change(Sender: TObject);
begin
If ((Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '')) Then
Button1.Enabled := True
Else
Button1.Enabled := False;
If ((Edit1.Text <> '') or (Edit2.Text <> '') or (Edit3.Text <> '') or (Edit4.Text <> '')) Then
Button2.Enabled := True
Else
Button2.Enabled := False;
end;
procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
If Not (((Key >= #48) and (Key <= #57)) or (Key = #8)) Then
Key := #0;
end;
end.

waaahh panjang yaaa... Begitulah salah satu program delphi untuk menyimpan data dari delphi ke file TXT.
Cukup sekian yaahh postingan kali ini.. trima kasih..