(VB.NET)将文本文件的下半部分显示到列表框中

问题描述 投票:0回答:1

我必须制作一个应用程序,用于组织跑步者及其团队的列表。在下面的文本文件中,我必须删除文本文件的上半部分(上半部分是列出的团队),并且只显示列表框项目中的下半部分(跑步者)。

文本文件:

 # School [School Code|School Name|Coach F-Name|Coach L-Name|AD F-Name|AD L Name]
 WSHS|Worcester South High School|Glenn|Clauss|Bret|Zane
 WDHS|Worcester Dorehty High School|Ellsworth|Quackenbush|Bert|Coco
 WBCHS|Worcester Burncoat High School|Gail|Cain|Kevin|Kane
 QRHS|Quabbin Regional High School|Bob|Desilets|Seth|Desilets  
 GHS|Gardner High School|Jack|Smith|George|Fanning   
 NBHS|North Brookfield High School|Hughe|Fitch|Richard|Carey
 WHS|Winchendon High School|Bill|Nice|Sam|Adams
 AUBHS|Auburn High School|Katie|Right|Alice|Wonderland
 OXHS|Oxford High School|Mary|Cousin|Frank|Daughter
 # Roster [Bib #|School Code|Runner's F-Name|Runner's L-Name]
 101|WSHS|Sanora|Hibshman
 102|WSHS|Bridgette|Moffitt
 103|WSHS|Karine|Chunn
 104|WSHS|Shanita|Wind
 105|WSHS|Fernanda|Parsell
 106|WSHS|Albertha|Baringer
 107|WSHS|Carlee|Sowards
 108|WDHS|Maisha|Kleis
 109|WDHS|Lezlie|Berson
 110|WDHS|Deane|Rocheleau
 111|WDHS|Hang|Hodapp
 112|WDHS|Zola|Dorrough
 113|WDHS|Shalon|Mcmonigle

我有一些代码从文本文件中读取每一行作为数组,并使用布尔变量来确定文本文件的结束位置。这只能显示我设法完成的团队。但我现在需要做相反的事情,只展示球员,我有点难过。

我的代码:

   Private Sub btnLoadTeams_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadTeam.Click
    ' This routine loads the lstTeam box from an ASCII .txt file
    ' # School [School Code | Name | Coach F-Name| Coach L-Name | AD F-Name | AD L-Name]

    Dim strRow As String
    Dim bolFoundCode As Boolean = False
    Dim bolEndCode As Boolean = False
    Dim bolFoundDup As Boolean = False
    Dim intPosition As Integer
    Dim intPosition2 As Integer
    Dim strTeamCodeIn As String
    Dim textIn As New StreamReader( _
        New FileStream(txtFilePath.Text, FileMode.OpenOrCreate, FileAccess.Read))
    ' Clear Team listbox
    lstTeam.Items.Clear()
    btnDeleteRunner.Enabled = True

    Do While textIn.Peek <> -1 And Not bolEndCode
        Me.Refresh()
        strRow = textIn.ReadLine.Trim
        If Not bolFoundCode Then
            If "# SCHOOL " = UCase(Mid(strRow, 1, 9)) Then
                bolFoundCode = True
            End If
        Else
            If Mid(strRow, 1, 2) <> "# " Then
                For Each item As String In lstTeam.Items
                    intPosition = InStr(1, strRow, "|")
                    strTeamCodeIn = Mid(strRow, 1, intPosition - 1)
                    intPosition2 = InStr(1, item, strTeamCodeIn)

                    If intPosition2 > 0 Then
                        bolFoundDup = True
                        MsgBox("Found Duplicate School Code: " & strTeamCodeIn)

                    End If
           Else
               bolEndCode = True
                Next

                If Not bolFoundDup Then
                    lstTeam.Items.Add(strRow)

                Else
                    lstTeam.Items.Add("DUPLICATE School Code: " & strRow)
                    lstTeam.Items.Add("Please correct input file and reload teams")
                    bolEndCode = True
                End If
            End If
        End If
    Loop

End Sub

我已经将bolEndCode = True放在读取文本文件中间部分的部分之间,但是所有Ive设法显示的是列表框中的以下内容:

 # Roster [Bib #|School Code|Runner's F-Name|Runner's L-Name]

任何帮助或提示如何只显示我的“lstPlayers”列表框中的跑步者将不胜感激。我是初学程序员,我们刚开始学习在.NET类中读取和编写数组。

arrays vb.net text-files read-write
1个回答
0
投票

首先我做了2个班,一个跑步者和一个学校。它们具有文本文件中可用的属性。作为课程的一部分,我添加了一个覆盖.ToString的函数。这是用于调用.ToString进行显示的列表框。

接下来,我创建了一个读取文件中所有数据的函数。使用File.ReadLines方法非常简单。

然后我创建了2个变量List(Of T)T代表Type。我们的类型是跑步者和学校。我使用List(Of T)而不是数组,因为我不必担心列表的大小。没有ReDim Preserve,只需继续添加项目。 FillList方法将数据添加到列表中。首先,我必须找到学校结束和跑步者开始的地方。我使用了Array.FindIndex方法,它有点不同,因为第二个参数是谓词。看一下。现在我们知道了我们想要为每个列表使用的行的索引,并使用For ... Next循环。在每个循环中,创建类的实例并设置属性。最后,新对象将添加到列表中。

最后,我们用简单的.AddRange和lists.ToArray填充列表框。请注意,我们正在添加整个对象,属性和所有内容。整洁的是我们可以从列表框项中访问属性。查看SelectedIndexChanged事件。您可以使用Runner列表框执行相同的操作。

抱歉,我无法使用您的代码。我几乎忘记了旧的vb6方法。 InStr,Mid等。当你可以使用.net方法时,它会更好。当老板说“用C#重写整个应用程序”时,它使你的代码更具可移植性

Public Class Runner
    Public Property BibNum As Integer
    Public Property SchoolCode As String
    Public Property FirstName As String
    Public Property LastName As String
    Public Overrides Function ToString() As String
        'The listbox will call .ToString when we add a Runner object to determin what to display
        Return $"{FirstName} {LastName}" 'or $"{LastName}, {FirstName}"
    End Function
End Class

Public Class School
    Public Property Code As String
    Public Property Name As String
    Public Property CoachFName As String
    Public Property CoachLName As String
    Public Property ADFName As String
    Public Property ADLName As String

    'The listbox will call .ToString when we add a School object to determin what to display
    Public Overrides Function ToString() As String
        Return Name
    End Function

End Class

Private Runners As New List(Of Runner)
Private Schools As New List(Of School)
Private Function ReadData(path As String) As String()
    Dim lines = File.ReadLines(path).ToArray
    Return lines
End Function

Private Sub FillLists(data As String())
    Dim location = Array.FindIndex(data, AddressOf FindRosterLine)
    'The first line is the title so we don't start at zero
    For index = 1 To location - 1
        Dim SplitData = data(index).Split("|"c)
        Dim Schl As New School
        Schl.Code = SplitData(0)
        Schl.Name = SplitData(1)
        Schl.CoachFName = SplitData(2)
        Schl.CoachLName = SplitData(3)
        Schl.ADFName = SplitData(4)
        Schl.ADLName = SplitData(5)
        Schools.Add(Schl)
    Next
    For index = location + 1 To data.GetUpperBound(0)
        Dim SplitData = data(index).Split("|"c)
        Dim Run As New Runner
        Run.BibNum = CInt(SplitData(0))
        Run.SchoolCode = SplitData(1)
        Run.FirstName = SplitData(2)
        Run.LastName = SplitData(3)
        Runners.Add(Run)
    Next
End Sub

Private Function FindRosterLine(s As String) As Boolean
    If s.Trim.StartsWith("# Roster") Then
        Return True
    Else
        Return False
    End If
End Function

Private Sub FillListBoxes()
    Dim arrRunners As Runner() = Runners.ToArray
    Dim arrSchools As School() = Schools.ToArray
    ListBox1.Items.AddRange(arrSchools)
    ListBox2.Items.AddRange(arrRunners)
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim arrRunner = ReadData("Runners.txt")
    FillLists(arrRunner)
    FillListBoxes()
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim Schl = DirectCast(ListBox1.SelectedItem, School)
    TextBox1.Text = Schl.CoachLName
    TextBox2.Text = Schl.Code
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.