0

我有一个文本文件格式:

  *******************************************************************************
    *                              Mitecs Test Plan                               *
    *******************************************************************************

    [PRODUCTN]
    FQA=3F.4.W0,41,1
    RSC=3F.5.W4,36,1
    CFG=3F.9.2J,234,1

    [MASTERREV]
    MTP=3F.R.WM

    [FQA 13]
    FQA=3F.4.W0,41,1
    CFG=3F.9.2J,263,1

    [FQA 14]
    FQA=3F.4.W0,160,1
    CFG=3F.9.2J,315,1

我想阅读文本并将其显示在列表框中,如下所示:

[PRODUCTN]
[MASTERREV]
[FQA 13]
[FQA 14]

从上图

从上图中,当我在列表框 1 中选择 [FQA 14] 项目并单击交换按钮时,它应在列表框 2 中以以下格式显示为

Code    Name    Version
160     FQA      3F.4.W0
315     CFG      3F.9.2J
4

1 回答 1

1

一种选择是使用一个类来保存每个条目并覆盖 ToString 函数以返回标题。现在您可以将每个条目直接添加到 listbox1 中,它将显示代表该项目的标题。由于每个列表框项实际上都是一个对象,因此您可以将选定的项转换为您的条目类并从对象中读取数据。这是一种方法:

Public Class Entry
    Public Property Title As String = ""
    Public Property Data As New List(Of String)
    Public Overrides Function ToString() As String
        Return Title
    End Function
End Class

Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sr As New StreamReader("textfile1.txt")
    Do Until (sr.EndOfStream)
        Dim line As String = sr.ReadLine.Trim
        If line.StartsWith("[") Then
            Dim newentry As New Entry
            newentry.Title = line
            Do Until (line = "" OrElse sr.EndOfStream)
                line = sr.ReadLine.Trim
                If Not line = "" Then
                    newentry.Data.Add(line)
                End If
            Loop
            ListBox1.Items.Add(newentry)
        End If
    Loop
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim selectedentry As Entry = DirectCast(DirectCast(sender, ListBox).SelectedItem, Entry)
    ListBox2.Items.Clear()
    For Each datum In selectedentry.Data
        Dim line As String() = datum.Split("=,".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        If line.Count > 2 Then
            ListBox2.Items.Add(line(2) + vbTab + line(0) + vbTab + line(1))
        Else
            ListBox2.Items.Add("   " + vbTab + line(0) + vbTab + line(1))
        End If
    Next
End Sub
于 2014-01-29T04:45:35.860 回答