I was following some code presented by another developer and for some reason the program is not reading the txt file that I created with the csv data.
What am I doing wrong?
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class ConverterUpdated
Dim UnitConvVal As Double ' Global Variable for Unit Value.
Public Sub DataLoad(ByVal Switch As String)
Dim tfLines() As String = File.ReadAllLines("C:\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)
DataLoad("SelectUnit")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
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
This is the information inside C:\data.txt
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