Code:
'untuk menggunakan stream reader, harus melakukan IMPORTS component
'yaitu System.IO
Imports System.IO
Public Class Form1
Private Sub btnGetData_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGetData.Click
'mengambil lokasi file exe
Dim myPath As String = AppDomain.CurrentDomain.BaseDirectory
Dim SR As StreamReader
Dim strBaris1, strBaris2, strBaris3 As String
'mengosongkan variabel
strBaris1 = ""
strBaris2 = ""
strBaris3 = ""
'mengosongkan stream reader
sr = Nothing
'mengecek apakah file myData.txt ada
If File.Exists(myPath & "myData.txt") Then
'SR = New StreamReader(myPath & "myData.txt")
'Dim sr As StreamReader = File.OpenText(myPath & "myData.txt")
SR = File.OpenText(myPath & "myData.txt")
strBaris1 = SR.ReadLine
strBaris2 = SR.ReadLine
strBaris3 = SR.ReadLine
Else
MessageBox.Show("File yang Anda maksud tidak ditemukan", _
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'SR.Close()
'Jika SR/StreamReader berisi atau tidak kosong,
'maka SR harus ditutup
If Not SR Is Nothing = True Then
SR.Close()
End If
'Jika semua variabel strBaris mempunyai isi
If strBaris1 <> "" And strBaris2 <> "" And strBaris3 <> "" Then
'sebelum mengisi data pada TextBox, ComboBox, ListBox
'kosongkan data terlebih dahulu
txtData.Text = ""
cboData.Items.Clear()
lstData.Items.Clear()
'mengisi txtData
'vbCrLf berfungsi untuk memberikan ENTER / baris baru
txtData.Text = strBaris1 & vbCrLf & _
strBaris2 & vbCrLf & _
strBaris3
'vbCrlF ==> visual basic Carriage return Line feed
'mengisi cboData
cboData.Items.Add(strBaris1)
cboData.Items.Add(strBaris2)
cboData.Items.Add(strBaris3)
'mengisi lstData
lstData.Items.Add(strBaris1)
lstData.Items.Add(strBaris2)
lstData.Items.Add(strBaris3)
End If
End Sub
End Class
Download Source Lengkap : DOWNLOAD
No comments:
Post a Comment