VB 2008 - 逐字过滤txt文件

时间:2013-11-23 06:26:09

标签: vb.net

好的,我正在研究一个学校项目。这就是我使用vb 2008的原因。除了一部分,我在项目上做了很多事情。我到处寻找答案,但仍然无法找到答案。所以我决定最后请求一些建议/帮助。感谢

enter image description here

enter image description here 问题: 因此,您在第一张图片的文本框中填写详细信息并点击保存。员工姓名保存在一个txt文件中。新行上的每个名称。

其余的文本框保存在一个名为report的txt文件中。每个文本框都有一个新行。

点击主菜单进入编辑员工窗口后。这是第二张图片。将出现员工列表窗口。它只是一个列表框。然后列表框读取employeenames txt文件并显示名称。我想要它,所以当您突出显示员工列表中的一个名称时,将读取report.txt中的相应信息并将其放入正确的文本框中。因此,您可以再次编辑和保存信息。

我需要解决的问题: 我想知道我最好的方法是逐行过滤和提取信息并将它们放在文本框中。

所以,当你突出名字尼古拉斯。他的信息将从txt文件中获取,正确的信息将放在正确的文本框中。此外,我计划使用列表框中的排序选项对员工列表中的名称进行排序。

我真的很陌生,所以如果我没有正确解释一些东西,那就减少了一些懈怠。感谢

2 个答案:

答案 0 :(得分:1)

以下是:

首先创建你的课程 班级员工 公共名称为String .... 最终课程

学习使用List(t)如何使用以及如何使用自己数据上的列表进行排序(你应该在排序和查找时实现iComparable接口或传递你的Icompare)或者自己编写这些函数

首先使用虚拟数据来查看是否所有内容都像您一样工作

然后最终保存并恢复您的数据。 你有很多选择 使用sql 使用简单的xml 等等

我建议你先使用xml。读取xml并编写它。或者您可以直接将数据序列化为xml文件,并在还原时对其进行反序列化 。

这是我在搜索时发现的内容。它使用Linq和xml序列化程序。顺便说一句,解决方案与你的解决方案完全相同。

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

这是我的简单解决方案。我用Linq并且没有Linq编写了排序和查找的方法。您需要存储数据。为此,请查看以上链接或来自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

答案 1 :(得分:0)

如果您这样做,文件将保存为Employees名称,那么您可以使用此方法

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

    ComboBox1.Items.Add(foundFile)
Next

这是我能看到的最简单的方法。它列出了文件夹中的所有文件。但是显示目录,所以如果你不想这样做,那就这样做:

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

然后从找到的文件和.txt中重新显示所有目录位置 - 如果您需要更多帮助,只需回复或PM我或其他东西,我希望它有所帮助:)

-nfell2009