【问题标题】:VB 2008 - Filter txt file word for wordVB 2008 - 逐字过滤 txt 文件
【发布时间】:2013-12-03 16:51:26
【问题描述】:

好吧,我现在正在做一个学校项目。这就是我使用 vb 2008 的原因。除了一部分之外,我已经完成了该项目的几乎所有工作。我到处寻找答案,但仍然找不到。所以我决定最终寻求一些建议/帮助。谢谢

问题: 因此,您在第一张图像的文本框中填写详细信息并点击保存。员工姓名保存在一个 txt 文件中。每个名称都换行。

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

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

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

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

我对此真的很陌生,所以如果我没有正确解释某些事情,请让我放松一下。谢谢

【问题讨论】:

  • 对于小型数据集(我认为是这种情况),您应该依赖内存而不是外部文件。也就是说,设置一个集合系统(数组、列表、字典等)或一个类(如 qwr 建议的那样)并将所有值存储在那里。例如:Dim employeeNames as List<string>Dim annualSalary as List<decimal>等。所有这些列表将被协调,您可以轻松显示所有相关信息。示例:Dim curIndex As Integer = employeeName.IndexOf("this company") -> 将此curIndex 与其他列表一起使用,您将获得相关信息。
  • 建议:数据库文件会更好吗?
  • @nfell2009 最快的选择总是内存(例如,数组);所以如果你可以依赖内存,因为数据的大小不是太重要,你应该更好地依赖内存。数据库非常快(虽然比内存慢),但还有其他相关问题(= 数据库必须安装在给定的计算机上,这会降低可移植性)。通过查看 OP 要求以及该项目的大小/复杂性,我将依赖内存;如果什么“数据库文件”,你的意思是一个不需要任何安装的易于访问的文件,我会选择它作为我的第二个选项。
  • @varocarbas 是的,我知道数组更快,但是数据库允许所有信息都可以通过多条记录进行存储和访问。加上将它存储在文本文件中也会停止可移植性,处理外部文件时总是会遇到这个问题。除非他们对现有文件进行错误检查,并且可能从网络服务器下载
  • @nfell2009 也许我没有说清楚:我不建议依赖数组,而是依赖内存(我以数组为例说明我的意思);即:在程序本身内部拥有程序正在处理的所有信息。用户写入的每一位都不会存储在文件或数据库中,而是存储在内存中。这是最快、最便携、适应性最强的选择;它唯一真正的限制是大小,但这似乎不是OP描述的问题。数据库在这里听起来不值得推荐,但如果你愿意,你可以把你的答案集中在那里......

标签: vb.net


【解决方案1】:

方法如下:

首先创建你的类 班级员工 公共名称作为字符串 …… 结束课

学习使用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

【讨论】:

  • 你能用一些代码解释一下吗?抱歉,我真的很新,我很难完全理解你在说什么:)
  • @NicholasMcLovinzRothapfel 我发布了一些示例。希望对您有所帮助
【解决方案2】:

如果您将文件保存为员工名称,则可以使用此方法

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

【讨论】:

  • 另一个答案的弱点是没有显示任何代码。您的回答表明完全不了解这一切的含义。 OP 说一切都从文件中很好地填充,但要求过滤(实际上它在标题中),意思是:如何同步不同 GUI 元素中显示的信息。示例:当从组合框中选择employee1(按照您的建议填充并且OP已经在执行)时,他希望在每个控件中显示与此记录相关的信息(他的年龄在年龄文本框中,他的性别在性别文本框等)
  • qwr 的回答不值得 +1;但就我不喜欢 -1ing 而言,我必须 +1 他只是为了补偿,因为这个答案太可怕了。
  • 我的列出了文件,它可能不是他想要的,但它适用于他可能能够使用的东西。如果他想要将所有信息存储在文本文件中,那么由于处理文本文件的方式是不可能的,或者需要大量代码
  • OP 想要用不同的信息填充不同类型的控件;如果您想依赖文件,您应该考虑必须协调的不同文件(例如,names.txt、ages.txt、salary.txt);或在同一个文件上,包括所有信息,并具有一定的分隔(例如,CSV 文件,其行包括:姓名、年龄、薪水等)。 OP 想知道的是如何同步所有这些信息并快速访问它。不是如何访问/编辑文件。正确阅读问题以及其他回答者所写的内容,以准确了解此问题的含义。
猜你喜欢
  • 2017-02-11
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多