通过VB.Net中包含的特定于循环的文本框行

通过VB.Net中包含的特定于循环的文本框行,vb.net,Vb.net,我有这个文本文件:有4个对应的文本框:TxtStringNumP1、TxtStringNumP2、txtstringnum3和TxtStringNumP4,我需要显示相应的值:在文本框中:也就是说,要显示这些值​​在相应的框中输入数字。这是我的文本文件的外观: [TxtStringNumP1] 4 5 2 4 [TxtStringNumP2] 5 10 4 6 6 5 10 如何编写此代码使其工作 Dim line As String Using reader As StreamReader

我有这个文本文件:有4个对应的文本框:TxtStringNumP1、TxtStringNumP2、txtstringnum3和TxtStringNumP4,我需要显示相应的值:在文本框中:也就是说,要显示这些值​​在相应的框中输入数字。这是我的文本文件的外观:

[TxtStringNumP1]
4
5
2
4
[TxtStringNumP2]
5
10
4
6
6
5
10
如何编写此代码使其工作

Dim line As String
Using reader As StreamReader = New StreamReader(My.Application.Info.DirectoryPath + ("\Number.txt")
        ' Read one line from file
         line = reader.ReadLine
         If(line.Contains("[TxtStringNumP1]") Then
             'The current line is the store hours header, so we skip it (read the next line)         
              line = reader.ReadLine
              'Process the line like you want, and keep processing through the lines by doing a readline each time you want to progress to the next line.
         End If
End Using
我必须让它看起来像这样:

TxtStringNumP1.Text=

TxtStringNumP2.Text=

其他代码:

如果要逐行应用某些逻辑,跳过不需要的行,然后逐行读取文件或在内存中读取后一次处理一行,则选择取决于文件大小

这种方法使用IEnumerable扩展,其中在ReadLines返回的行上,ReadLines返回您的行的IEnumerable,而不将它们全部加载到内存中

For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
    Dim lines = File.ReadLines(f.FullName).
                Where(Function(x) Not x.Contains("Kleur"))
    ' lines contains only the lines without the word Kleur
    For Each l As String In lines
        ' Process the lines
    Next
Next
但您也可以使用StreamReader读取一行,如果需要,对其进行处理,然后循环下一行

Dim line As String = ""
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
    Using sr = New StreamReader(f.FullName)
        While True
            line = sr.ReadLine
            If line IsNot Nothing Then
                If Not line.Contains("Kleur") Then
                    ' process the line
                End If
            Else
                Exit While
            End If 
        End While
    End Using
Next
最后,您可以加载内存中的所有内容并从中进行处理,但要注意文件的大小

Dim line As String = ""
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
    Dim lines = File.ReadAllLines(f.FullName)
    For each line in lines
       if Not line.Contains("Kleur") Then
         ' process the line
       End If
    Next
Next
您从StreamReader开始,所以我使用了这种方法

我正在使用StringBuilder为文本框积累字符串。与字符串不同,StringBuilder是可变的。每次我们向字符串添加内容时,都会丢弃旧字符串并创建新字符串。如果有多个字符串,则StringBuilder效率更高

我读了第一行,然后开始外部Do循环。首先,我们找到要使用Controls集合填充的文本框。我去掉前导括号和尾随括号,得到作为文本框名称的字符串

内部Do一直循环,直到到达文件末尾或找到下一个括号。在StringBuilder上调用ToString并将其添加到文本框中。StringBuilder被清除,我们转到外部Do的顶部,得到下一个文本框

外部循环在文件末尾退出

Private Sub OPCode()
    Dim line As String
    Using reader As StreamReader = New StreamReader(My.Application.Info.DirectoryPath & "\TestReader.txt")
        line = reader.ReadLine
        Dim sb As New StringBuilder
        Do
            Dim tb = CType(Controls(line.Trim("["c).Trim("]"c)), TextBox)
            Do
                If reader.Peek < 0 Then 'Check that you haven't reached the end
                    Exit Do
                End If
                line = reader.ReadLine
                If line.StartsWith("["c) Then 'Check if we have reached another check box.
                    Exit Do
                End If
                sb.AppendLine(line)
            Loop
            tb.Text = sb.ToString
            sb.Clear()
        Loop Until reader.Peek < 0
    End Using
End Sub

谢谢你的帮助,一切都很好,很快,我可以学到新东西。
Dim line As String = ""
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
    Dim lines = File.ReadAllLines(f.FullName)
    For each line in lines
       if Not line.Contains("Kleur") Then
         ' process the line
       End If
    Next
Next
Private Sub OPCode()
    Dim line As String
    Using reader As StreamReader = New StreamReader(My.Application.Info.DirectoryPath & "\TestReader.txt")
        line = reader.ReadLine
        Dim sb As New StringBuilder
        Do
            Dim tb = CType(Controls(line.Trim("["c).Trim("]"c)), TextBox)
            Do
                If reader.Peek < 0 Then 'Check that you haven't reached the end
                    Exit Do
                End If
                line = reader.ReadLine
                If line.StartsWith("["c) Then 'Check if we have reached another check box.
                    Exit Do
                End If
                sb.AppendLine(line)
            Loop
            tb.Text = sb.ToString
            sb.Clear()
        Loop Until reader.Peek < 0
    End Using
End Sub