Kodingnya kawan…
Imports System.IO
Imports
System.IO.File
Public Class Form2
Const PATH As String = "D:\supplier.txt"
Const TEMP As String = "D:\temp.txt"
Dim kode,
nama, alamat, telepon As String
Dim reader As StreamReader
Dim writer As StreamWriter
Dim r As BinaryReader
Sub tampil()
Me.dgvTampil.Rows.Clear()
reader = File.OpenText(PATH)
Dim i As Integer = 0
While Not reader.EndOfStream
Me.dgvTampil.Rows.Add()
With
Me.dgvTampil.Rows(i)
.Cells(0).Value =
reader.ReadLine
.Cells(1).Value =
reader.ReadLine
.Cells(2).Value =
reader.ReadLine
.Cells(3).Value =
reader.ReadLine
End
With
i += 1
End While
reader.Close()
End Sub
Sub
bersih()
Me.txtTelepon.Clear()
Me.txtAlamat.Clear()
Me.txtNama.Clear()
Me.txtKode.Clear()
Me.txtKode.Focus()
End Sub
Function
periksaSupplier(ByVal kodeCari As String) As Boolean
reader = File.OpenText(PATH)
While Not reader.EndOfStream
If
kodeCari.ToUpper = reader.ReadLine.ToString.ToUpper Then
reader.Close()
Return
True
End
If
End While
reader.Close()
Return False
End Function
Function
koleksiSupplier(ByVal kodeCari As String) As ArrayList
Dim
supp As New
ArrayList
reader = File.OpenText(PATH)
While Not reader.EndOfStream
kode = reader.ReadLine
nama = reader.ReadLine
alamat = reader.ReadLine
telepon = reader.ReadLine
If
kodeCari.ToUpper = kode.ToUpper Then
supp.Add(nama)
supp.Add(alamat)
supp.Add(telepon)
reader.Close()
Return
supp
End
If
End While
reader.Close()
Return Nothing
End Function
Function
periksaInput(ByVal kodeInput As String, ByVal namaInput As String, ByVal
alamatInput As String,
ByVal teleponInput As
String) As Boolean
If
kodeInput.Trim.Length = 0 Then
MsgBox("Kode
harus diisi")
Return
False
ElseIf
namaInput.Trim.Length = 0 Then
MsgBox("Nama
harus diisi")
Return
False
ElseIf
alamatInput.Trim.Length = 0 Then
MsgBox("Alamat
harus diisi")
Return
False
ElseIf
teleponInput.Trim.Length = 0 Then
MsgBox("Telepon
harus diisi")
Return
False
End If
Return True
End Function
Sub rename(ByVal oldName As String, ByVal newName
As String)
File.Delete(newName)
File.Copy(oldName, newName)
File.Delete(oldName)
End Sub
Sub
tambah()
If
periksaSupplier(Me.txtKode.Text) = True Then
MsgBox("Supplier
sudah ada")
Exit
Sub
End If
If
periksaInput(Me.txtKode.Text, Me.txtNama.Text, Me.txtAlamat.Text,
Me.txtTelepon.Text) = False
Then
Exit
Sub
End If
writer = File.AppendText(PATH)
writer.WriteLine(Me.txtKode.Text)
writer.WriteLine(Me.txtNama.Text)
writer.WriteLine(Me.txtAlamat.Text)
writer.WriteLine(Me.txtTelepon.Text)
writer.Close()
End Sub
Sub ubah()
If
periksaSupplier(Me.txtKode.Text) = False Then
MsgBox("Supplier
belum ada")
Exit
Sub
End If
If
periksaInput(Me.txtKode.Text, Me.txtNama.Text, Me.txtAlamat.Text,
Me.txtTelepon.Text) = False
Then
Exit
Sub
End If
reader = File.OpenText(PATH)
writer = File.AppendText(TEMP)
While Not reader.EndOfStream
kode = reader.ReadLine
nama = reader.ReadLine
alamat = reader.ReadLine
telepon = reader.ReadLine
If
kode.ToUpper = Me.txtKode.Text.ToUpper Then
writer.WriteLine(Me.txtKode.Text)
writer.WriteLine(Me.txtNama.Text)
writer.WriteLine(Me.txtAlamat.Text)
writer.WriteLine(Me.txtTelepon.Text)
Else
writer.WriteLine(kode)
writer.WriteLine(nama)
writer.WriteLine(alamat)