temp3

Imports System.IO

Public Class Form1

    'Create private varables

    Private addressDList As Dictionary(Of String, List(Of String))

    Private lstAddress As List(Of Address)

    Private fileName As String = "address.txt"

    Private fs As FileStream

    Private textIn As StreamReader

    Private textOut As StreamWriter

    Private addressItem As Address

    Private searchTerm As String

    Private tempAddress As Address

    'Find adress function

    Private Function FindAddress(ByVal nickName As String) As Address

        For Each addr As Address In lstAddress

            If addr.NickName = nickName Then

                Return addr

            End If

        Next

        'return nothing

        Return Nothing

    End Function

    'create the subroutene for saveAllAddress

    Private Sub SaveAllAddress()

        Try

            'if the file exists. Delete it

            If File.Exists(fileName) Then

                File.Delete(fileName)

            Else

                'Create new file if it does not exist

                File.Create(fileName)

            End If

            'Create a new file if the file is not there

            fs = New FileStream(fileName, FileMode.Create)

 

            textOut = New StreamWriter(fs)

            'Go through each of the values in the file

            For Each kvp As KeyValuePair(Of String, List(Of String)) In addressDList

                Dim addressEntry = New Address(kvp.Key, kvp.Value.Item(0), kvp.Value.Item(1))

                textOut.WriteLine(addressEntry.NickName + "," + addressEntry.FullName + "," + addressEntry.Email)

            Next

            'close the file

            textOut.Close()

 

        Catch ex As IOException

            MessageBox.Show(ex.Message, "IOException")

        Finally

            If fs IsNot Nothing Then

                fs.Close()

            End If

        End Try

    End Sub

    'All the form load functions

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

        addressDList = New Dictionary(Of String, List(Of String))

        lstAddress = New List(Of Address)

        'If the address file exists read the file

        If File.Exists(fileName) Then

            Try

                fs = New FileStream(fileName, FileMode.Open)                 textIn = New StreamReader(fs)                 Do While textIn.Peek <> -1

                    'reads the line of the file that was opened

                    Dim row As String = textIn.ReadLine

                    Dim columns() As String = row.Split(CChar(","))

                    'if the contents does not exist add to the file

                    If Not addressDList.ContainsKey(columns(0)) Then

                        Dim lstNameEmail = New List(Of String)

                        lstNameEmail.Add(columns(1) + "," + columns(2))

                        lstNameEmail.Add(columns(3))

                        addressDList.Add(columns(0), lstNameEmail)

                        addressItem = New Address(columns(0), columns(1) + ", " + columns(2), columns(3))

                        lstAddress.Add(addressItem)

                    End If

                Loop

                textIn.Close()

                'close the file

                DataGridView1.DataSource = lstAddress

                'Catch if the file is not found

            Catch ex As FileNotFoundException

                MessageBox.Show(fileName + "not found.", "File Not Found")

            Catch ex As IOException

                MessageBox.Show(ex.Message, "IOException")

            Finally

                If fs IsNot Nothing Then

                    fs.Close()

                End If

            End Try

        End If

    End Sub

    'On click event for the search     Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Dim searchTerm As String

        searchTerm = txtSearch.Text.Trim

        'if the search term is blank

        If searchTerm = "" Then

            MessageBox.Show("Search Text is Blank")

        Else

            lstAddress = Nothing

        End If

 

        lstAddress = New List(Of Address)

        'checks the input in the search box

        For Each kvp As KeyValuePair(Of String, List(Of String)) In addressDList

            Dim name(1) As String

            name(0) = kvp.Value.Item(0)

            Dim nameArray(1) As String

            nameArray = Split(name(0), ",", 2)

 

            Dim firstName As String

            Dim lastName As String

            Dim email As String

            'Trims off any extras

            firstName = nameArray(1)

            lastName = nameArray(0)

            firstName = firstName.Trim

            lastName = lastName.Trim

            email = kvp.Value.Item(1)

            email = email.Trim

 

 

 

            tempAddress = New Address(kvp.Key, kvp.Value.Item(0), kvp.Value.Item(1))

            'conditions for the search

            If searchTerm = "*" Then

                lstAddress.Add(tempAddress)

            ElseIf searchTerm = kvp.Key Then

                lstAddress.Add(tempAddress)

            ElseIf searchTerm = firstName Then

                lstAddress.Add(tempAddress)

            ElseIf searchTerm = lastName Then

                lstAddress.Add(tempAddress)

            ElseIf searchTerm = email Then

                lstAddress.Add(tempAddress)

            End If

        Next

        'return nothing if there is no result

        DataGridView1.DataSource = Nothing

        DataGridView1.DataSource = lstAddress

    End Sub

    'when the form is closing

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        'Calls the saveAllAddress sub

        SaveAllAddress()

    End Sub

    'click event for the intert button

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click

        'Checks each of the text feild for blanks

        If txtNick.Text = "" Then

            MessageBox.Show("NickName is blank")

        ElseIf txtName.Text = "" Then

            MessageBox.Show("Name is blank")

        ElseIf txtEmail.Text = "" Then

            MessageBox.Show("Email is blank")

        Else

            'pass all of the values to varables

            Dim nick As String

            nick = txtNick.Text.Trim

            Dim name As String

            name = txtName.Text.Trim

            Dim email As String

            email = txtEmail.Text.Trim

            'Places everything into an array

            Dim tempAddress = New Address(nick, name, email)

            'If the name is there then we will repace it and update

            If addressDList.ContainsKey(nick) Then

                Dim removeAddress As Address

                removeAddress = FindAddress(nick)

                addressDList.Remove(removeAddress.NickName)

                lstAddress.Remove(removeAddress)

            End If

            'Stores the name into a string

            Dim lstNameEmail = New List(Of String)

            lstNameEmail.Add(tempAddress.FullName)

            lstNameEmail.Add(tempAddress.Email)

            addressDList.Add(tempAddress.NickName, lstNameEmail)

            lstAddress.Add(tempAddress)

            'Displays noting if the result was not entered.

            DataGridView1.DataSource = Nothing

            DataGridView1.DataSource = lstAddress

        End If

    End Sub

  End Class

posted @ 2015-06-17 15:41  xymum  阅读(115)  评论(0编辑  收藏  举报