将ID及其信息扔到listview中

Throw ID and its information into listview

我有多行文本框和 ListView 文本框包含:

[1000]
姓名=约翰
number0=78569987

[1001]
名字=莎拉
number0=89768980
number1=77897545



TextBox2.Text = TextBox2.Text.Replace("[", "this what i want")

Dim lines As New List(Of String)
lines = TextBox2.Lines.ToList
Dim FilterText = "this what i want"
For i As Integer = lines.Count - 1 To 0 Step -1
    If Not Regex.IsMatch(lines(i), FilterText) Then
        lines.RemoveAt(i)
    End If
Next
TextBox2.Lines = lines.ToArray
TextBox2.Text = TextBox2.Text.Replace("this what i want", "")
TextBox2.Text = TextBox2.Text.Replace("]", "")

ListBox1.Items.AddRange(TextBox2.Lines)

For Each x As String In ListBox1.Items
    Dim II As New ListViewItem
    II.Text = x
    ListView1.Items.Add(II)
Next

我不能用同样的方法插入数字和名字,因为有些 ID 包含数字 0 数字 1 而有些只包含数字 0,那么我该如何插入它们的数字?

提前致谢。

检查下面的代码和注释。您可能需要稍微修改以适应数据。
检查 this question and it's linked questions 是否有类似的东西。

Edit:请注意,这只会从 richtextbox 中的行复制到 listview 中的不同列,因此它适用于您提供的示例。我希望您可以根据 richtextbox 中的数据改进此逻辑以说明特定列。

Dim lines As New List(Of String)
lines = TextBox2.Lines.ToList

'Add 1st row to the listview
ListView1.Items.Add(New ListViewItem())

'Use Counter to determine row#
Dim j As Integer = 0

'Loop through the items
For i As Integer = 0 To lines.Count - 1
    'Check if it's 1st item i.e. ID and add as text (i.e. at Index 0)
    If lines(i).StartsWith("[") Then
        ListView1.Items(j).Text = lines(i).Substring(1, lines(i).Length - 2)

    'Check if contains other columns with attributes
    ElseIf lines(i).Contains("=") Then
        ListView1.Items(j).SubItems.Add(lines(i).Substring(lines(i).IndexOf("=") + 1))

    'Check if it's an empty record, and add new row to listview
    Else
        j = j + 1
        ListView1.Items.Add(New ListViewItem())
    End If
Next