VB 2008 - Filter txt file word for word

Ok well I am working on a school project at the moment. Thats why I am using vb 2008. I have done pretty much everything on the project except one part. I have searched everywhere for an answer and still cant find one. So I decided to finally ask for some advice/help. Thanks

enter image description here

enter image description here The problem: So you fill in the details in the textboxes in the first image and hit save. The employee names are saved into one txt file. Each name on a new line.

The rest of the textboxes are saved into one txt file called report. Each textbox has a new line.

Once you hit main menu and go into the edit employee window. Which is the second image. The Employee list window will come up. It is just a list box. The list box then reads the employeenames txt file and displays the names. I want it so when you highlight one of the names from the employee list the corresponding info from report.txt will be read and placed into the correct text boxes. So you can edit and save the info once again.

what I need to work out: I want to know the best way for me to filter and pull info line by line and place them in a textbox.

So when you highlight the name nicholas. his info will be taken from the txt file and the correct info be placed in the correct textbox. Also I am planning on using the sorted option in the list box to sort the names in employee list.

Im really new to this so cut me some slack if I didnt explain something correctly. Thanks

Upvotes: 0

Views: 382

Answers (2)

qwr
qwr

Reputation: 3670

Here is how to:

first create your classes Class employee public name as String .... end class

learn using List(t) how to use and how to sort using list on your own data ( you should implement iComparable interface or pass your Icompare when sorting and finding) or write those function by yourself

work with dummy data first to see if everything working like you want

then final saving and restoring your data. You got many choices using sql using simple xml and so on

I advise you to use xml first. reading xml and writing it. or u can directly serialize your data to xml file and deserialize it when restoring .

Here is the what I found while searching. It uses Linq and xml serializer . And by the way that solution is merely same as yours.

http://www.codeproject.com/Articles/26652/Using-LINQ-to-Objects-in-Visual-Basic

And Here is my simple solution. I wrote it sorting and finding both way with Linq And without Linq .And you will need to store data . For that purpose check above link or this from msdn msdn serialize example

Imports System.Linq

    Module Module1

        Sub Main()
            Dim employee_list As New List(Of Employee)

            employee_list.Add(New Employee("Abdurrauf", 1, 1, "programmer"))
            employee_list.Add(New Employee("John", 5, 2, "programmer"))
            employee_list.Add(New Employee("Teylor", 10, 3, "programmer"))
            employee_list.Add(New Employee("John", 9, 4, "student"))
            employee_list.Add(New Employee("Jorj", 6, 5, "programmer"))
            employee_list.Add(New Employee("Samir", 1, 6, "programmer"))
            employee_list.Add(New Employee("Orxan", 3, 7, "programmer"))


            'sort by annual and display
            employee_list.Sort(Employee.GetSorter(SortType.AnualDesc))
            Console.WriteLine("Sorted by annual leave  {descending}:")
            For Each x As Employee In employee_list
                Console.WriteLine("   Name : {0} Annual : {1} ", x.Name, x.Annual_leave)

            Next
            'SORTING WITH LINQ  
            'using LINQ (this time you dont need to write sort class and you can avoid using it)
            Dim employeesByNameDesc = From x In employee_list _
                                      Order By x.Name Descending _
                                      Select x

            Console.WriteLine("Sorted with using Linq by Name Descending")
            For Each x As Employee In employeesByNameDesc
                Console.WriteLine("   Name : {0} Annual : {1} ", x.Name, x.Annual_leave)

            Next

            'find by name without lambda
            Dim em As Employee = findemp(employee_list  ,"Samir")
            If em IsNot Nothing Then
                Console.WriteLine("found : emp Name {0} desc {1} ", em.Name, em.Description)
            End If


            'find by name with lambda
            Dim emp As Employee = employee_list.Find(Function(x) (x.Name = "John"))
            If emp IsNot Nothing Then
                Console.WriteLine("found : emp Name {0} desc {1} ", emp.Name, emp.Description)
            End If



            Console.Read()


        End Sub

        Function findemp(emlist As List(Of Employee), name As String) As Employee
            For Each x In emlist
                If (x.Name = name) = True Then
                    Return x
                End If
            Next
            Return Nothing 

        End Function

        <Serializable()> _
        Class Employee
            Implements IComparable(Of Employee)


            Private _Name As String
            Private _Annual_leave As Integer
            Private _Sick_leave As Integer
            Private _Description As String

            Sub New(name As String, ann As Integer, sl As Integer, desc As String)
                With Me
                    ._Name = name
                    .Annual_leave = ann
                    .Sick_leave = sl
                    .Description = desc
                End With
            End Sub





            Property Name As String
                Get
                    Return _Name
                End Get
                Set(value As String)
                    _Name = value

                End Set
            End Property
            Property Description As String
                Get
                    Return _Description
                End Get
                Set(value As String)
                    _Description = value

                End Set
            End Property

            Property Annual_leave As Integer
                Get
                    Return _Annual_leave
                End Get
                Set(value As Integer)
                    _Annual_leave = value

                End Set
            End Property

            Property Sick_leave As Integer
                Get
                    Return _Sick_leave
                End Get
                Set(value As Integer)
                    _Sick_leave = value

                End Set
            End Property


            'default compare
            Public Overloads Function CompareTo(ByVal other As Employee) As Integer _
            Implements IComparable(Of Employee).CompareTo
                If other Is Nothing Then Return 1
                Return Name.CompareTo(other.Name)

            End Function

            Public Shared Function GetSorter(sortType As SortType) As IComparer(Of Employee)
                Return CType(New sortClass(sortType), IComparer(Of Employee))
            End Function



        End Class


        ''our comparer

        Public Enum SortType
            AnualAsc
            AnualDesc
            SickAsc
            SickDesc
            NameAsc
            NameDesc
        End Enum

        Private Class sortClass
            Implements IComparer(Of Employee)

            Dim _type As SortType
            Private _sortType As SortType

            Sub New(sortType As SortType)
                _sortType = sortType
            End Sub


            Private Function compareint(xx As Integer, yy As Integer) As Integer
                If (xx < yy) = True Then
                    Return 1
                ElseIf (xx > yy) = True Then
                    Return -1
                Else
                    Return 0
                End If

            End Function


            Public Overloads Function Compare(x As Employee, y As Employee) As Integer _
         Implements IComparer(Of Employee).Compare
                Dim res As Integer = 0

                Select Case _type
                    Case SortType.NameAsc
                        res = String.Compare(x.Name, y.Name)
                    Case SortType.NameDesc
                        res = String.Compare(y.Name, x.Name)
                    Case SortType.AnualAsc
                        res = compareint(x.Annual_leave, y.Annual_leave)
                    Case SortType.AnualDesc
                        res = compareint(y.Annual_leave, x.Annual_leave)
                    Case SortType.SickAsc
                        res = compareint(x.Sick_leave, y.Sick_leave)
                    Case SortType.SickDesc
                        res = compareint(y.Sick_leave, x.Sick_leave)
                    Case Else
                        res = String.Compare(x.Name, y.Name)
                End Select

                Return res
            End Function

        End Class
    End Module

Upvotes: 1

BaeFell
BaeFell

Reputation: 650

If you make it so the file is saved as the Employees name, then you can use this method

Dim DirectoryLocation As String = "YOUR DIRECTORY HERE"
For Each foundFile As String In My.Computer.FileSystem.GetFiles(
    DirectoryLocation)

    ComboBox1.Items.Add(foundFile)
Next

That is the simplest way I can see to do this. It lists all the files in the folder. But shows the directory so if you don't want that then do this:

Dim DirectoryLocation As String = "YOUR DIRECTORY HERE" 
'Example: "C:\temp\" It must END in a \

For Each foundFile As String In My.Computer.FileSystem.GetFiles(
    DirectoryLocation)


    foundFile = foundFile.Replace(DirectoryLocation, Nothing)
    foundFile = foundFile.Replace(".txt", Nothing)
    ComboBox1.Items.Add(foundFile)
Next

That then repalces all the directory location from the files found and the .txt - I hope it helps if you need any more help just reply or PM me or something :)

-nfell2009

Upvotes: 0

Related Questions