Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在中读取文件并输出到两个列表框_Vb.net_File Io - Fatal编程技术网

Vb.net 在中读取文件并输出到两个列表框

Vb.net 在中读取文件并输出到两个列表框,vb.net,file-io,Vb.net,File Io,我正在Windows窗体应用程序中使用Imports System.IO和StreamReader 我试图获取一个文件,读入它,并将其输出到两个列表框中。文本文件的格式如下所示 Blue, 23.7 Green, 60.1 Black, 45.3 我想将值大于50的颜色输出到一个列表框中,将值小于50的颜色输出到另一个列表框中。到目前为止,我所做的只是将整个列表输出到一个文本框中。其代码如下所示: srTextFile = File.OpenText(dataFile) Do While s

我正在Windows窗体应用程序中使用Imports System.IO和StreamReader

我试图获取一个文件,读入它,并将其输出到两个列表框中。文本文件的格式如下所示

Blue, 23.7
Green, 60.1
Black, 45.3
我想将值大于50的颜色输出到一个列表框中,将值小于50的颜色输出到另一个列表框中。到目前为止,我所做的只是将整个列表输出到一个文本框中。其代码如下所示:

srTextFile = File.OpenText(dataFile)

Do While srTextFile.EndOfStream = False
    'read file by line, use the comma as a splitter
    thisFile = srTextFile.ReadLine().Split(",")
    For i As Integer = 0 To thisFile.GetUpperBound(0)
        txtFileDisplay.AppendText(thisFile(i) &vbTab)
    Next
    txtFileDisplay.AppendText(vbCrLf)
Loop
 For Each line As String In File.ReadAllLines("Your file here")
     Dim Spl() AS String = Split(line, ",")
     'Convert string value to integer
     Dim myNum As Double = Double.Parse(Spl(1))'The number is the second item in the array
     If myNum < 50.0 Then
          'Add to your first Listbox here using
          'Listbox.Add(myNum)
     Else
          'Add to your second Listbox here using
          'Listbox.Add(myNum)
     End If
 Next 
我对阅读文件是完全陌生的。我真的不知道我在做什么。我对数组也很陌生

谢谢

您可以使用System.IO.File类来执行此操作

只需将文本文件读入字符串即可 把绳子分成几行 将行拆分为数组 将字符串解析为双精度 比较double并将大于50的值放入单独的列表框中 您可以这样编写代码:

srTextFile = File.OpenText(dataFile)

Do While srTextFile.EndOfStream = False
    'read file by line, use the comma as a splitter
    thisFile = srTextFile.ReadLine().Split(",")
    For i As Integer = 0 To thisFile.GetUpperBound(0)
        txtFileDisplay.AppendText(thisFile(i) &vbTab)
    Next
    txtFileDisplay.AppendText(vbCrLf)
Loop
 For Each line As String In File.ReadAllLines("Your file here")
     Dim Spl() AS String = Split(line, ",")
     'Convert string value to integer
     Dim myNum As Double = Double.Parse(Spl(1))'The number is the second item in the array
     If myNum < 50.0 Then
          'Add to your first Listbox here using
          'Listbox.Add(myNum)
     Else
          'Add to your second Listbox here using
          'Listbox.Add(myNum)
     End If
 Next 

通过使用类,可以创建包含颜色名称和双值的对象,并将其添加到列表框中

Public Class ColorValue
    Public Property Name As String
    Public Property Value As Double

    Public Overrides Function ToString() As String
        Return $"{Name} ({Value})"
    End Function
End Class
注意,我已经重写了ToString,因为ListBox使用它来显示每个项目的文本

现在,您可以像这样向列表框添加颜色

For Each line As String In File.ReadLines(dataFile)
    Dim parts As String() = line.Split(","c)
    If parts.Length = 2 Then 'If line is well-shaped.
        Dim value As Double
        Double.TryParse(Trim(parts(1)), value) 'Gets 0 into value if conversion fails.
        Dim color = New ColorValue With {.Name = parts(0), .Value = value}
        If value > 50.0 Then
            listBox1.Items.Add(color)
        Else
            listBox2.Items.Add(color)
        End If
    End If
Next
现在,您可以使用返回颜色值

Dim c As ColorValue = DirectCast(listBox1.SelectedItem, ColorValue)
Dim n As String = c.Name
Dim v As Double = c.Value

你能包含任何你尝试过的代码吗?像这样的成对数组是一种反模式。。。要避免的事情。创建类并读入该类类型的单个数组或列表要好得多。而且这些就是数据的确切例子吗?标识符字段本身是否有可能包含逗号?您可以编辑问题以添加它。您可能会发现一个有用的集合和类很酷。首先我想说声谢谢,这真的很有帮助!是否可以立即将文件读入myLines,而无需先将其全部读入长字符串?@user10778077我已经编辑了这篇文章。现在,您可以直接读入myLines甚至可以跳过将行读入myLines,方法是在File.ReadLinesfile.txt中将每行用作字符串。ReadLines不会将行存储在中间集合中。取而代之的是,它会在你每次迭代时读取文件。谢谢@OlivierJacot Descombes,我已经对答案进行了更改