我有一个文本文件格式:
*******************************************************************************
* 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
答案 0 :(得分: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