在两个不同的文本框中显示名称和电话号码

时间:2013-11-03 07:39:26

标签: .net vb.net member

到目前为止,这是我的代码,我有一个列表框,显示从文本文件中读取的名称。然后我有两个文本框:txtName和txtPhone。当名称在列表框中突出显示时,它应显示txtName中的名称和txtPhone中的手机nunber,但它不能正常工作。任何帮助表示赞赏。

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim memberphones() As String = IO.File.ReadAllLines("memberphones.csv")
        Dim query = From line In memberphones
                    Let data = line.Split(","c)
                    Let name = data(0)
                    Let phone = data(1)
                    Select name
        lstOutput.DataSource = query.ToList
        txtName.Text = Name
        txtPhone.Text = phone
    End Sub

End Class

这是我的文本文件的内容: Carol Preiss,587-2333 Alice Rees,860-9744 Carlos Sanchez,209-4587 约翰史密斯,576-2988

2 个答案:

答案 0 :(得分:1)

创建一个类来保存值并使您的查询创建该类的实例:

Public Class Form1

    Public Class Record
        Public Name As String
        Public Phone As String

        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim query = From line In IO.File.ReadAllLines("memberphones.csv")
                    Let data = line.Split(","c)
                    Select New Record() With {.Name = data(0), .Phone = data(1)}
        lstOutput.DataSource = query.ToList
    End Sub

    Private Sub lstOutput_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstOutput.SelectedIndexChanged
        If lstOutput.SelectedIndex <> -1 Then
            Dim rec As Record = lstOutput.SelectedItem
            txtName.Text = rec.Name
            txtPhone.Text = rec.Phone
        End If
    End Sub

End Class

答案 1 :(得分:0)

当列表框 selectionchanged 时,请为其设置一个事件处理程序。 在事件处理程序中,您可以获得实际选定项目及其适当性。 将eventhandler中的这些属性添加到两个文本框

如果您准备好了,但仍有问题,请检查您的变量。他们都有正确的价值观?