Simpan dan Baca Data .TXT Visual Basic .NET







siapkan seperti  di bawah ini:
NO
TOOLBOX
NAME
1
Label 1
nik
2
Label 2
nama
3
Label 3
tgl lahir
4
Label 4
jabatan
5
Label 5
tanggal join
6
Text Box 1
input nik
7
Text Box 2
input nama
8
Datetimepicker 1
input tanggal lahir
9
Datetimepicker 2
input tanggal join
10
Combobox1
jabatan
11
Button 1
simpan
12
Button 2
baca
13
Button 3
cek usia
14
Button 4
lama kerja




Public Class Form1
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Label12.Text = ComboBox1.Text.Substring(10)
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim FILE_NAME As String = "d:\textTM4.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then
            System.IO.File.Delete(FILE_NAME)
        End If
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
        objWriter.Write("NIK              :" & TextBox1.Text & vbCrLf)
        objWriter.Write("Nama             :" & TextBox2.Text & vbCrLf)
        objWriter.Write("Tanggal Lahir    :" & DateTimePicker1.Value.ToString("dd'- 'MMM' - 'yyyy") & vbCrLf)
        objWriter.Write("Jabatan          :" & ComboBox1.Text & vbCrLf)
        objWriter.Write("Tanggal Join     :" & DateTimePicker2.Value.ToString("dd'- 'MMM' - 'yyyy") & vbCrLf)
        objWriter.Write("Usia             :" & Label6.Text & Label7.Text & Label8.Text & vbCrLf)
        objWriter.Write("Lama Kerja       :" & Label9.Text & Label10.Text & Label11.Text & vbCrLf)

        objWriter.Close()

        MsgBox("Text berhasil disimpan", vbInformation, "Sukses")

        TextBox1.Text = ""
        TextBox2.Text = ""
        ComboBox1.Text = ""
        Label6.Text = ""
        Label7.Text = ""
        Label8.Text = ""
        Label9.Text = ""
        Label10.Text = ""
        Label11.Text = ""
        Label12.Text = ""

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim FILE_NAME As String = "Hasil.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            TextBox1.Text = objReader.ReadLine.Substring(18)
            TextBox2.Text = objReader.ReadLine.Substring(18)
            DateTimePicker1.Value = Convert.ToDateTime(objReader.ReadLine.Substring(18))
            ComboBox1.Text = objReader.ReadLine.Substring(18)
            DateTimePicker2.Value = Convert.ToDateTime(objReader.ReadLine.Substring(18))
        Else
            MsgBox("File tidak ditemukan", MsgBoxStyle.Critical, "Perhatian")

        End If

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If (Len(TextBox1.Text) = 0 Or Len(TextBox2.Text) = 0) Then
            MsgBox("Kolom NIK atau Nama tidak Boleh kosong", MsgBoxStyle.Critical, "Perhatian")
        Else
            Label6.Text = DateDiff("yyyy", DateTimePicker1.Value, Now).ToString
            Label7.Text = DateDiff("m", DateTimePicker1.Value, Now).ToString
            Label8.Text = DateDiff("d", DateTimePicker1.Value, Now).ToString
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If (Len(TextBox1.Text) = 0 Or Len(TextBox2.Text) = 0) Then
            MsgBox("Kolom NIK atau Nama tidak Boleh kosong", MsgBoxStyle.Critical, "Perhatian")
        Else
            Label9.Text = DateDiff("yyyy", DateTimePicker2.Value, Now).ToString
            Label10.Text = DateDiff("m", DateTimePicker2.Value, Now).ToString
            Label11.Text = DateDiff("d", DateTimePicker2.Value, Now).ToString
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call isi_combo()
    End Sub
    Sub isi_combo()
        ComboBox1.Items.Add("Operation Manager")
        ComboBox1.Items.Add("Operation Supervisor")
        ComboBox1.Items.Add("Operation Foreman")
        ComboBox1.Items.Add("Operation Staff")
        ComboBox1.Items.Add("Operation Admin")
    End Sub
End Class

Komentar