Program Visual Basic untuk menyimpan file txt
3. Pilih Windows Form Application
Object
|
Text
|
Name
|
Label 1
|
First Name
| |
Label2
|
Middle Name
| |
Label3
|
Last Name
| |
Label4
|
Employee Number
| |
Label5
|
Departemen
| |
Label6
|
Telephone
| |
Label7
|
Extension
| |
Label8
|
Email Address
| |
TextBox1
|
txtFirstName
| |
TextBox2
|
txtMiddleName
| |
TextBox3
|
txtLastName
| |
TextBox4
|
txtEmployeeNumber
| |
ComboBox1
|
cboDepartment
| |
TextBox5
|
txtTelephone
| |
TextBox6
|
txtExtension
| |
TextBox7
|
txtEmail
| |
Button
|
&Save and Record
|
btnSave
|
Button
|
C&lear
|
btnClear
|
Button
|
E&xit
|
btnExit
|
Gunakan Script berikut :
Imports System.IO Public Class frmEmployeeDataPart1 ' Declare global variables Dim fileName As String Dim firstName As String Dim middleName As String Dim lastName As String Dim employeeNumber As Integer Dim department As String Dim telephone As String Dim extension As Integer Dim emailAddress As String Dim valid As Boolean = True Private Sub frmEmployeeDataPart1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Do fileName = InputBox("Input Needed", "Enter the name of the file.") If fileName = Nothing Or fileName = "" Then MessageBox.Show("No file name entered.") Else Exit Do End If Loop End Sub Private Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click valid = True ' reset value to account for prior invalid input InputData() If valid = True Then WriteDataToFile() Else InputData() End If End Sub Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click txtFirstName.Text = "" txtMiddleName.Text = "" txtLastName.Text = "" txtEmployeeNumber.Text = "" cboDepartment.SelectedIndex = -1 txtTelephone.Text = "" txtExtension.Text = "" txtEmail.Text = "" End Sub Private Sub btnExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExit.Click Me.Close() End Sub Sub InputData() firstName = txtFirstName.Text If (firstName.ToUpper >= "A") And (firstName.ToUpper <= "Z") Then firstName = firstName Else valid = False MessageBox.Show("First Name must start with a letter") End If middleName = txtMiddleName.Text If (middleName.ToUpper >= "A") And (middleName.ToUpper <= "Z") Then middleName = middleName Else valid = False MessageBox.Show("Middle Name must start with a letter") End If lastName = txtLastName.Text If (lastName.ToUpper >= "A") And (lastName.ToUpper <= "Z") Then lastName = lastName Else valid = False MessageBox.Show("Last Name must start with a letter") End If Try employeeNumber = CInt(txtEmployeeNumber.Text) Catch MessageBox.Show("You must enter an integer for Employee Number.") valid = False End Try If cboDepartment.SelectedIndex <> -1 Then department = cboDepartment.Text Else valid = False MessageBox.Show("You must select a department.") End If telephone = txtTelephone.Text Try extension = CInt(txtExtension.Text) Catch MessageBox.Show("You must enter an integer for Extension.") valid = False End Try emailAddress = txtEmail.Text End Sub Sub WriteDataToFile() ' Menyimpan data Dim sw As StreamWriter = File.AppendText(fileName) sw.WriteLine(firstName) sw.WriteLine(middleName) sw.WriteLine(lastName) sw.WriteLine(employeeNumber) sw.WriteLine(department) sw.WriteLine(telephone) sw.WriteLine(extension) sw.WriteLine(emailAddress) sw.Close() MessageBox.Show("Data telah disimpan, jika ingin input data lagi klik Clear.") End Sub End Class
Program Visual Basic Untuk Membaca File txt
1. Buka Visual Basic 2010 anda seperti gambar di bawah ini
2. Kemudian klik " New project " Seperti gambar di bawah ini
3. Setelah di klik maka akan mucul gambar di bawah ini kemudian klik " Windows Form Application "
4. Kemudian akan muncul form kosong seperti gambar di bawah ini
5. Setelah itu anda sorot ke arah kiri pada form ada atau cari " Toolbox" kemudian pilih " Button " , " TextBox ", " Label " seperti gambar di bawah ini
6. maka hasilnya akan muncul form seperti gambar di bawah ini
CATATAN : "Ganti semua properti seperti ListBox1, Button1 dan yang lainnya menjadi seperti gambar di bawah ini "
Object
|
Text
|
Name
|
Label1
|
Record Number
| |
Label2
|
First Name
| |
Label3
|
Middle Name
| |
Label4
|
Last Name
| |
Label5
|
Employee Number
| |
Label6
|
Departemen
| |
Label7
|
Telephone
| |
Label8
|
Extension
| |
Label9
|
Email Address
| |
TextBox1
|
lblFirstName
| |
TextBox2
|
lblMiddleName
| |
TextBox3
|
lblLastName
| |
TextBox4
|
lblEmployeeNumber
| |
ComboBox1
|
lblDepartment
| |
TextBox5
|
lblTelephone
| |
TextBox6
|
lblExtension
| |
TextBox7
|
lblEmail
| |
Button
|
Next Record
|
btnNext
|
Button
|
C&lear
|
btnClear
|
Button
|
E&xit
|
btnExit
|
Imports System.IO Public Class frmEmployeeDataPart2 ' Declare global variables Dim recordNumber As Integer = 1 Dim filename As String Dim sr As StreamReader Dim message As String Dim fileValid As Boolean = False Private Sub frmEmployeeDataPart2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load ' Ask the user for the name of the text file and opens the file ' Checks user input to make sure that the file name is valid Do filename = InputBox("Input Needed", "Enter the name of the file.") If File.Exists(filename) Then sr = File.OpenText(filename) fileValid = True Else message = "Either no file has yet been created or the file " message &= "is not where expected." MessageBox.Show(message, "File Not Found") fileValid = False End If Loop Until fileValid = True End Sub Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click ' clears all of the fields when the Clear button is pressed lblRecordNumber.Text = "" lblFirstName.Text = "" lblMiddleName.Text = "" lblLastName.Text = "" lblEmployeeNumber.Text = "" lblDepartment.Text = "" lblTelephone.Text = "" lblExtension.Text = "" lblEmailAddress.Text = "" End Sub Private Sub btnExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExit.Click Me.Close() End Sub Private Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click If sr.EndOfStream = True Then MessageBox.Show("End of file") Else lblRecordNumber.Text = CStr(recordNumber) lblFirstName.Text = sr.ReadLine lblMiddleName.Text = sr.ReadLine lblLastName.Text = sr.ReadLine lblEmployeeNumber.Text = sr.ReadLine lblDepartment.Text = sr.ReadLine lblTelephone.Text = sr.ReadLine lblExtension.Text = sr.ReadLine lblEmailAddress.Text = sr.ReadLine recordNumber += 1 End If End Sub End Class