从.text visual basic添加类到listbox的实例

时间:2013-11-23 05:32:21

标签: vb.net visual-studio-2012 listbox

Visual Basic的新功能。我正在尝试从csv文件中读取,拆分它,然后仅在索引(0)处获取值并将它们全部添加到列表框中。这就是我得到的。任何我很欣赏的建议。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim data() As String
        Dim initFileContents() As String = IO.File.ReadAllLines("MembershipPhone.txt")
        data = initFileContents.First.Split(","c)
        name = data(0)
        phone = data(1)
        ListBox1.Items.Add(name)
        TextBox1.Text = name
        TextBox2.Text = phone
End Sub

当然这只会添加第一个实例。我需要C ++相当于“对于数据中的每一行......”

1 个答案:

答案 0 :(得分:0)

只需使用For循环迭代ReadAllLines()返回的行:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim data() As String
    For Each line As String In IO.File.ReadAllLines("MembershipPhone.txt")
        data = line.Split(","c)
        Name = data(0)
        phone = data(1)
        ListBox1.Items.Add(Name)
        TextBox1.Text = Name
        TextBox2.Text = phone
    Next
End Sub