Combobox not populating with values from text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xicer
    New Member
    • Dec 2010
    • 13

    #1

    Combobox not populating with values from text file

    Hi guys

    need your help with a project im working on.

    I have 2 comboboxes that are not getting the values from a CSV text file.

    I have no errors during debug.


    CSV text file text
    Code:
     Category,Weight
    Category,Distance
    Weight,LB to KG,0.45359237
    Weight,KG to LB,2.20462262
    Distance,Mile to KM,1.609344
    Distance,KM to Mile,0.621371192

    Code

    Code:
     Option Strict On
    Option Explicit On
    Public Class Form1
        Dim UnitConvVal As Double ' Global Variable for Unit Value.
    
        Public Sub DataLoad(ByVal Switch As String)
            Dim tfLines() As String = System.IO.File.ReadAllLines("data.txt") ' File to load.
            For Each line As String In tfLines ' Load and read all lines in file.
                Dim field As String() = line.Split(","c) ' Split using ,.
    
                Select Case Switch
                    Case "LoadCategory"
                        If field(0) = "Category" Then
                            cboCategory.Items.Add(field(1))
                            cboCategory.SelectedIndex = 0 ' Auto Select first Category.
                        End If
    
                    Case "LoadUnits"
                        If field(0) = CStr(cboCategory.SelectedItem) Then
                            cboConvertTo.Items.Add(field(1))
                            cboConvertTo.SelectedIndex = 0 ' Auto Select first Unit.
                        End If
    
                    Case "SelectUnit"
                        If field(1) = CStr(cboConvertTo.SelectedItem) Then
                            UnitConvVal = CDbl(field(2)) ' Load Unit's Conversion value, store in UnitConvVal as Double.
                        End If
                End Select
            Next
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
            DataLoad("LoadCategory") ' Switch to DataLoad to load Categories on form load.
        End Sub
    
        Private Sub cboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            cboConvertTo.Items.Clear() ' Clear cboConvertTo Combobox when re-loading units after selection change.
            DataLoad("LoadUnits")
        End Sub
    
        Private Sub cboConvertTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboConvertTo.SelectedIndexChanged
            DataLoad("SelectUnit")
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            If txtInputVal.Text = Nothing Then
                MessageBox.Show("Please Enter A Value to Convert") ' Display message if no value is entered.
            Else
                txtResult.Text = CStr(CDbl(txtInputVal.Text) * UnitConvVal) ' If Value has been entered, compute conversion for unit.
            End If
        End Sub
    End Class


    Thanks in advance
Working...