By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Combobox not populating with values from text file

P: 13
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
Expand|Select|Wrap|Line Numbers
  1.  Category,Weight
  2. Category,Distance
  3. Weight,LB to KG,0.45359237
  4. Weight,KG to LB,2.20462262
  5. Distance,Mile to KM,1.609344
  6. Distance,KM to Mile,0.621371192

Code

Expand|Select|Wrap|Line Numbers
  1.  Option Strict On
  2. Option Explicit On
  3. Public Class Form1
  4.     Dim UnitConvVal As Double ' Global Variable for Unit Value.
  5.  
  6.     Public Sub DataLoad(ByVal Switch As String)
  7.         Dim tfLines() As String = System.IO.File.ReadAllLines("data.txt") ' File to load.
  8.         For Each line As String In tfLines ' Load and read all lines in file.
  9.             Dim field As String() = line.Split(","c) ' Split using ,.
  10.  
  11.             Select Case Switch
  12.                 Case "LoadCategory"
  13.                     If field(0) = "Category" Then
  14.                         cboCategory.Items.Add(field(1))
  15.                         cboCategory.SelectedIndex = 0 ' Auto Select first Category.
  16.                     End If
  17.  
  18.                 Case "LoadUnits"
  19.                     If field(0) = CStr(cboCategory.SelectedItem) Then
  20.                         cboConvertTo.Items.Add(field(1))
  21.                         cboConvertTo.SelectedIndex = 0 ' Auto Select first Unit.
  22.                     End If
  23.  
  24.                 Case "SelectUnit"
  25.                     If field(1) = CStr(cboConvertTo.SelectedItem) Then
  26.                         UnitConvVal = CDbl(field(2)) ' Load Unit's Conversion value, store in UnitConvVal as Double.
  27.                     End If
  28.             End Select
  29.         Next
  30.  
  31.     End Sub
  32.  
  33.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
  34.         DataLoad("LoadCategory") ' Switch to DataLoad to load Categories on form load.
  35.     End Sub
  36.  
  37.     Private Sub cboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  38.         cboConvertTo.Items.Clear() ' Clear cboConvertTo Combobox when re-loading units after selection change.
  39.         DataLoad("LoadUnits")
  40.     End Sub
  41.  
  42.     Private Sub cboConvertTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboConvertTo.SelectedIndexChanged
  43.         DataLoad("SelectUnit")
  44.     End Sub
  45.  
  46.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
  47.         If txtInputVal.Text = Nothing Then
  48.             MessageBox.Show("Please Enter A Value to Convert") ' Display message if no value is entered.
  49.         Else
  50.             txtResult.Text = CStr(CDbl(txtInputVal.Text) * UnitConvVal) ' If Value has been entered, compute conversion for unit.
  51.         End If
  52.     End Sub
  53. End Class
  54.  


Thanks in advance
Jul 3 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.