0

Here is my code so far, I have a list box which shows the name read from a text file. Then I have two text box: txtName and txtPhone. When the name is highlighted in the list box, it should show the name in the txtName and the phone nunber in the txtPhone but it is not working as it should. Any help is appreciated.

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

here is the content of my text file: Carol Preiss,587-2333 Alice Rees,860-9744 Carlos Sanchez,209-4587 John Smith,576-2988

Krekkon
  • 1,349
  • 14
  • 35
user2844903
  • 1
  • 1
  • 1

2 Answers2

1

Create a Class to hold the values and make your query create instances of that class:

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
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • idle_mind, this exactly what i wanted. I appreciate your help. Fred – user2844903 Nov 05 '13 at 04:27
  • idle_mind, I added a delete button and a private sub but it is giving me an error: System.argumentexception. here is the code i added: Private Sub mnuUpdateDelete_Click(sender As Object, e As EventArgs) Handles mnuUpdateDelete.Click Dim searchStr = txtName.Text Dim ind As Integer = lstOutput.Items.IndexOf(searchStr) lstOutput.Items.RemoveAt(ind) End Sub – user2844903 Nov 05 '13 at 04:44
0

When the listbox selectionchanged, take an eventhandler for it. In the eventhandler, you can get the actual selected item, and its properites. Add these properties in the eventhandler to the two textbox

If you are ready with that and something is still wrong, please check your variables. All of them have the right values?

Krekkon
  • 1,349
  • 14
  • 35