首页 文章

如何用逗号从文本文件中拆分数组,只在Listbox中显示一个数组索引?

提问于
浏览
0

首先,我要感谢愿意提前帮助的人 . 我的问题在于,当我尝试使用数组中的项目填充列表框时,我只将“常规”添加为列表框项而不是数组中的实际字符串 . 我对编程很新,所以我很抱歉,如果这是我的代码中的一个明显的错误,虽然我假设它可能是 .

这是我的代码:

Private Sub frmSalesSummary_Load(sender As Object, e As EventArgs) Handles 
MyBase.Load
    Dim info() As String = IO.File.ReadAllLines("info.txt")
    Dim splitChar As Char = ","
    Dim infoString As String
    For i = 0 To info.GetUpperBound(0)
        infoString = info(i).Split(splitChar)
        lstSummary.Items.Add(infoString(i))
    Next
End Sub

文本文件包含此文件,用于在第一行测试“alex,General,7,$ 105.00” .

可能会频繁编辑这个问题,因为我也在积极尝试解决这个问题 . 道歉 .

2 回答

  • 1

    'info'是你的行(逗号分隔)'infoString'是由Split分隔的单词数组

    因此,如果你想从该分割中添加一个项目,你可以使用infoString(0)作为名称,使用infoString(1)作为Rank,等等......

    因此,您将获取每一行,将其拆分并将名称添加到列表框中,您只需要添加infoString(0),因为该数组的元素保留了将该名称从行中拆分的名称 .

    尝试

    lstSummary.Items.Add(infoString(0))
    

    如果你想要一个像这样的列表:

    alex
    General
    7
    $105.00
    

    您可以在当前循环中创建另一个循环,并使用迭代器作为索引 .

    For i = 0 to info.GetUpperbound(0)
       infoString = info(i).Split(splitChar)
       For x = 0 to infoString.GetUpperBound(0)
          lstSummary.Items.Add(infoString(x))
       Next
    Next
    

    这将占用每一行,拆分它,然后将每行的信息添加到列表框中...请记住,你将拥有你展示的4,然后下一行将放下接下来的4条信息,直到你用完为止文本文件中的行 .

    仍然不太确定这是否正是你想要做的,如果情况仍然如此,你可能会模拟一些你期望的图片以及是否还有更多这样的信息文本文件中的信息 .

  • 0

    一个小小的提示 . 您永远不应该使用控件来存储您正在使用的数据 . 将其存储为 PersonDetails 对象列表,或任何您想要调用它们的对象 .

    您可以像这样声明 PersonDetails 对象..

    Private Class PersonDetails
        Public Property Name As String
        Public Property Category As String
        Public Property Type As Integer
        Public Property Amount As Decimal
    End Class
    

    他们的名单就像这样

    Dim PersonSummaryList As New List(Of PersonDetails)
    

    表单加载事件读取文本文件,对于每一行,创建一个新的 PersonDetails 对象并将其添加到 PersonDetails 列表中,然后将名称添加到ListBox

    Private Sub frmSalesSummary_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim info() As String = IO.File.ReadAllLines("info.txt")
        Dim splitChar As Char = ","c
        Dim infoString() As String
        For i As Integer = 0 To info.GetUpperBound(0)
            infoString = info(i).Split(splitChar)
            'remove the commas and $ signs
            infoString(3) = infoString(3).Replace("$"c, "")
            Dim newPerson As New PersonDetails
            newPerson.Name = infoString(0)
            newPerson.Category = infoString(1)
            newPerson.Type = Integer.Parse(infoString(2))
            Decimal.TryParse(infoString(3), newPerson.Amount)
            PersonSummaryList.Add(newPerson)
        Next
    
    End Sub
    

    在ListBox中,您将看到的是一个名称列表,当您单击一个项目时,下面的代码将找到与您选择的名称匹配的对象,并在MessageBox中显示您想要的任何属性,但当然您可以用 tempPerson 对象做任何你想做的事 .

    Private Sub LstSummary_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LstSummary.SelectedIndexChanged
        Dim tempPerson As PersonDetails = PersonSummaryList.Find(Function(x) x.Name = LstSummary.SelectedItem.ToString)
        MessageBox.Show("Name :" & tempPerson.Name & vbCrLf & "Amount :" & tempPerson.Amount)
    End Sub
    

相关问题