对于显示列表框中的每一行,我需要将行分为子串:年份和温度,转换它们并将它们的值插入并行列表。我非常沮丧,因为我确信这很简单,但我已经整晚都在为这个项目工作。
我正在使用的列表框数据来自我导入的csv文件并包含看起来像这样的数据
1999,6.654616
2000,8.616851
2001,6.61681
这是我目前的代码。
'Open File Dialog
Dim dlg As New OpenFileDialog With {
.Filter = "CSV Files|*.csv|Text Files|*.txt|All Files|*.*",
.Title = "Please select CSV Data",
.InitialDirectory = "C:\"
}
dlg.ShowDialog()
'Declaring streamreader object
R = New IO.StreamReader(dlg.FileName)
While (R.Peek() > -1)
ListBoxDisplay.Items.Add(R.ReadLine)
End While
R.Close()
'Testing
'List
Dim year As New List(Of Integer)
Dim avgtemp As New List(Of Double)
答案 0 :(得分:0)
String.Split应该适合你。
Private Sub GetLists()
Dim year As New List(Of Integer)
Dim avgtemp As New List(Of Double)
While (R.Peek() > -1))
Dim line As String = R.ReadLine
ListBoxDisplay.Items.Add(line)
Dim strArray() As String = line.Split(","c)
year.Add(CInt(strArray(0)))
avgtemp.Add(CDbl(strArray(1).Trim))
End While
R.Close()
End Sub