Untuk saat ini Program untuk mengedit dan menghapus data berbentuk TXT belum saya postingkan, tapi postingan kali ini akan menjelaskan bagaimana senuah program bisa membuat dan menyimpan data di txt dengan menggunakan 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;

Lumayan panjang yaahh... itu belum seberapa dengan program yang saya buat untuk Aplikasi Pembayaran Listrik desa. panjang banget...
udah dulu yaaa, lain kali saya akan lengkapi deh postingannya..
Wassalammualaikum..