none
(VB) load a file matching string value of selected listbox item RRS feed

  • Question

  • I'm trying to load a file using the string value of a combobox selected item. The text match and the variable description properly grabs the customer name from the list but my variables saved in a .hdf folder with customer name isn't working.
    Private Sub lstName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstName.SelectedIndexChanged
            'Adds customer information to the labels on the form and changes 
            ChDir(c)
            lstDevice.Items.Clear()
            lstComment.Items.Clear()
            Dim description As String = ""
    
            description = lstName.SelectedItem.ToString()
            Dim allFiles = Directory.EnumerateFiles(c + "\" + description + ".hdf")
    
            For Each Str As String In allFiles
    
                Dim values() As String = File.ReadAllText(Str).Split("|"c)
                firstname = values(0)
                lastname = values(1)
                homephone = values(2)
                cellphone = values(3)
                address = values(4)
                town = values(5)
                state = values(6)
                zipcode = values(7)
                email = values(8)
            Next
        End Sub

    Friday, December 7, 2018 12:42 AM

Answers

  • Imports System.IO
    
    Public Class Form1
        'Dim firstname
        'Dim lastname
        'Dim homephone
        'Dim cellphone
        'Dim address
        'Dim town
        'Dim state
        'Dim zipcode
        'Dim email
        Private Sub lstName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstName.SelectedIndexChanged
            Dim description As String = "test"
    
            'description = lstName.SelectedItem.ToString()
            Dim allFiles = Directory.EnumerateFiles("c\")
    
            For Each Str As String In allFiles
                Dim fil As FileInfo = New FileInfo(Str)
                If fil.Name = lstName.SelectedItem.ToString() + ".hdf" Then
                    Dim values() As String = File.ReadAllText(Str).Split("|"c)
                    firstname.Text = values(0)
                    lastname.Text = values(1)
                    homephone.Text = values(2)
                    cellphone.Text = values(3)
                    address.Text = values(4)
                    town.Text = values(5)
                    state.Text = values(6)
                    zipcode.Text = values(7)
                    email.Text = values(8)
                End If
    
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by DavidBasara Friday, December 7, 2018 9:57 PM
    Friday, December 7, 2018 8:41 AM

All replies

  • Hi,

    fix your code:

      Dim allFiles = Directory.EnumerateFiles("c/")
    
            For Each Str As String In allFiles
                Dim fil As FileInfo = New FileInfo(Str)
                If fil.Name = lstName.SelectedItem.ToString() + ".hdf" Then
                    Dim values() As String = File.ReadAllText(Str).Split("|"c)
                    firstname = values(0)
                    lastname = values(1)
                    homephone = values(2)
                    cellphone = values(3)
                    address = values(4)
                    town = values(5)
                    state = values(6)
                    zipcode = values(7)
                    email = values(8)
                End If

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 7, 2018 1:35 AM
  • It doesn't seem to update the labels. I programmed a conversion to lbl(name).text to the timer so it should update once the variable is loaded.
    • Edited by DavidBasara Friday, December 7, 2018 6:37 AM
    Friday, December 7, 2018 6:35 AM
  • Imports System.IO
    
    Public Class Form1
        'Dim firstname
        'Dim lastname
        'Dim homephone
        'Dim cellphone
        'Dim address
        'Dim town
        'Dim state
        'Dim zipcode
        'Dim email
        Private Sub lstName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstName.SelectedIndexChanged
            Dim description As String = "test"
    
            'description = lstName.SelectedItem.ToString()
            Dim allFiles = Directory.EnumerateFiles("c\")
    
            For Each Str As String In allFiles
                Dim fil As FileInfo = New FileInfo(Str)
                If fil.Name = lstName.SelectedItem.ToString() + ".hdf" Then
                    Dim values() As String = File.ReadAllText(Str).Split("|"c)
                    firstname.Text = values(0)
                    lastname.Text = values(1)
                    homephone.Text = values(2)
                    cellphone.Text = values(3)
                    address.Text = values(4)
                    town.Text = values(5)
                    state.Text = values(6)
                    zipcode.Text = values(7)
                    email.Text = values(8)
                End If
    
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by DavidBasara Friday, December 7, 2018 9:57 PM
    Friday, December 7, 2018 8:41 AM
  • I had to change "dim description as string= lastname + " " + firstname and it worked PERFECTLY! Thanks for the help!
    Friday, December 7, 2018 9:58 PM