Search code examples
vb.netcsvimportcomboboxstreamreader

Importing a CSV into a combobox, then changing other things in VB


What I want to do is import a CSV file (called fwlist.txt), that looks like this:

modelname,power type,pic,part number,firmware, option1, option 1, etc

End result, i would like a combobox that shows the modelname, and when the model name is selected from the pulldown, it updates various labels and text boxes on the form with other information.

Here's what I have so far:

Dim filename As String = "fwlist.txt"
Dim pwrtype As String
Dim pic As String
Dim partnum As String
Dim lineread As String
Dim FirmwareName As String



Private Sub ReadFirmwaresLoad(sender As Object, e As EventArgs) Handles Me.Load
    ' Load the items into the NameComboBox list.
    Dim ResponseDialogResult As DialogResult

    Try

        Dim FirmwareStreamReader As StreamReader = New StreamReader(filename)
        ' Read all the elements into the list.
        Do Until FirmwareStreamReader.Peek = -1
            lineread = FirmwareStreamReader.ReadLine()
            Dim fields As String() = lineread.Split(",")
            FirmwareName = fields(0) 'Take First Field
            cbFW.Items.Add(FirmwareName)
            'Set Text labels based on position in line. 
            pwrtype = fields(1)
            pic = fields(2)
            partnum = fields(3)
            (...etc through options)
        Loop
        ' Close the file.
        FirmwareStreamReader.Close()

    Catch ex As Exception
        ' File missing.
        ResponseDialogResult = MessageBox.Show("File not Found!", "File Not Found",
            MessageBoxButtons.OK, MessageBoxIcon.Question)
        If ResponseDialogResult = DialogResult.OK Then
            ' Exit the program.
            Me.Close()
        End If
    End Try
End Sub

Private Sub cbFW_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbFW.SelectedIndexChanged
    lblPwrType.Text = pwrtype
    lblPic.Text = pic
    lblPartNum.Text = parnum
    ....etc thruogh options
End Sub

This code works, but only sort of. If i select anything from the combo box, it only gives me the information from the very last line of the CSV file - even if its the first entry in the box. I'm pretty sure it's something simple that I'm messing up.. anyone help?


Solution

  • First we read the lines into Firmware objects then we set this List(Of Firmware) as the DataSource of the ComboBox.

    Then we handle the SelectedIndexChanged event of the ComboBox which will get the currently selected firmware and loads its data to the TextBox controls.

    This is a tested, working example below:

    Public Class Firmware
    
        Public Property ModelName As String
        Public Property PowerType As String
        Public Property Pic As String
        Public Property PartNumber As String
        Public Property Firmware As String
        Public Property Option1 As String
    
    End Class
    
    Public Class MainForm
    
        Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
    
            Dim lines As String() = Nothing
    
            Try
                ' Read the file in one step
                lines = File.ReadAllLines("fwlist.txt")
            Catch ex As Exception
    
                Dim dialogResult As DialogResult = MessageBox.Show(Me, "File not found! Would you like to exit program?", "Error reading file", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
    
                If dialogResult = DialogResult.Yes Then
                    Me.Close()
                End If
    
                Exit Sub
    
            End Try
    
            Dim firmwares As List(Of Firmware) = New List(Of Firmware)
    
            For Each line As String In lines
    
                Dim rawData As String() = line.Split({","}, StringSplitOptions.None)
    
                ' Create Firmware object from each line
                Dim firmware As Firmware = New Firmware() With
                {
                    .ModelName = rawData(0),
                    .PowerType = rawData(1),
                    .Pic = rawData(2),
                    .PartNumber = rawData(3),
                    .Firmware = rawData(4),
                    .Option1 = rawData(5)
                }
    
                ' We store the read firmwares into a list
                firmwares.Add(firmware)
    
            Next
    
            ' Set the list as the data source of the combobox
            ' DisplayMember indicates which property will be shown in the combobox
            With cboModelNames
                .DataSource = firmwares
                .DisplayMember = "ModelName"
            End With
    
        End Sub
    
        Private Sub cboModelNames_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboModelNames.SelectedIndexChanged
    
            RefreshForm()
    
        End Sub
    
        Private Sub RefreshForm()
    
            ' Get the selected item as Firmware object
            Dim currentFirmware As Firmware = DirectCast(cboModelNames.SelectedItem, Firmware)
    
            ' Refresh all the textboxes with the information
            With currentFirmware
                txtPowerType.Text = .PowerType
                txtPic.Text = .Pic
                txtPartNumber.Text = .PartNumber
                txtFirmware.Text = .Firmware
                txtOption1.Text = .Option1
            End With
    
        End Sub
    
    End Class