Subscribe:

Saturday, November 6, 2010

Stream Writer dan Mencari Data dari Notepad

Jurusan akan otomatis terisi jika 2 karakter dari NPM mulai dari karakter ke 3 sesuai dengan kode jurusannya.
Search akan memunculkan inputbox dan mencari data dari file Notepad (.txt)


Contoh Code:
Public Class Form1

    'procedure untuk mengosongkan textbox
    Sub clearInput()
        txtNPM.Text = ""
        txtNama.Text = ""
        txtKelas.Text = ""
        txtJurusan.Text = ""
        txtNPM.Focus()
    End Sub

    'procedure untuk membuat textbox readonly true / false
    Sub makeReadOnly(ByVal tf As Boolean)
        txtNPM.ReadOnly = tf
        txtNama.ReadOnly = tf
        txtKelas.ReadOnly = tf
    End Sub

    Function getJurusan(ByVal NPM As String) As String
        Dim KodeJurusan, Jurusan As String
        KodeJurusan = NPM.Substring(2, 2)
        KodeJurusan = Microsoft.VisualBasic.Mid(NPM, 3, 2)

        '0631097
        'th-kd-urt

        Jurusan = ""
        If KodeJurusan = "11" Then
            Jurusan = "Teknik Sipil"
        ElseIf KodeJurusan = "21" Then
            Jurusan = "Teknik Elektro"
        ElseIf KodeJurusan = "31" Then
            Jurusan = "Sistem Informasi"
        ElseIf KodeJurusan = "41" Then
            Jurusan = "Manajemen"
        ElseIf KodeJurusan = "42" Then
            Jurusan = "Akuntasi"
        ElseIf KodeJurusan = "43" Then
            Jurusan = "Manajemen Perhotelan"
        ElseIf KodeJurusan = "44" Then
            Jurusan = "Magister Manajemen"
        ElseIf KodeJurusan = "51" Then
            Jurusan = "Ilmu Hukum"
        Else
            Jurusan = "Belum terdaftar"
        End If
        Return Jurusan
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        makeReadOnly(True)
        clearInput()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        makeReadOnly(False)
        clearInput()
    End Sub

    Private Sub txtNPM_Leave(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles txtNPM.Leave

        If txtNPM.Text.Trim = "" Then Exit Sub
        txtJurusan.Text = getJurusan(txtNPM.Text)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim flag As Boolean = False
        Dim NPM As String
        Dim tNPM, tNama, tKelas, tJurusan As String
        NPM = InputBox("Silahkan masukkan NPM", "Input")
        Do While NPM.Length <> 7 And NPM.Length > 1
            NPM = InputBox("Silahkan masukkan NPM", "Input")
        Loop
        'Do
        '    NPM = InputBox("Silahkan masukkan NPM", "Input")
        'Loop Until NPM.Length = 7
        'If NPM.Length <> 7 Then Exit Sub
        Dim sr As IO.StreamReader = IO.File.OpenText("mahasiswa.txt")
        Do While sr.Peek <> -1
            tNPM = sr.ReadLine
            tNama = sr.ReadLine
            tKelas = sr.ReadLine
            tJurusan = sr.ReadLine
            If tNPM = NPM Then
                flag = True
                txtNPM.Text = tNPM
                txtNama.Text = tNama
                txtKelas.Text = tKelas
                txtJurusan.Text = tJurusan
            End If
        Loop
        sr.Close()
        If flag = False Then
            MessageBox.Show("Data tidak ditemukan")
        End If
    End Sub
End Class

No comments: