簡體   English   中英

通過循環特定的文本框行包含在 VB.Net

[英]Through Loop specific Textboxes lines contains in VB.Net

我有這個文本文件:有 4 個對應的文本框:TxtStringNumP1、TxtStringNumP2、TxtStringNumP3 和 TxtStringNumP4,我需要顯示對應的值:在 Textboxes 中:即在相應的框中顯示這些值(數字)。 這是我的文本文件的樣子:

[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 =

4
5
2
4

TxtStringNumP2.Text =

5
10
4
6

其他代碼:

如果你想逐行應用一些邏輯跳過不需要的行,然后逐行讀取文件或在 memory 中讀取它們后一次處理一行(選擇取決於文件大小)

這種方法在 ReadLines 返回的行上使用 IEnumerable 擴展 Where (返回行的 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

最后,您可以加載 memory 中的所有內容並從那里處理(但要注意文件的大小)

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來累積文本框的字符串。 與 String 不同, StringBuilder是可變的(可變的)。 每次我們向字符串添加一些東西時,舊的字符串被丟棄並創建一個新的字符串。 如果有多個字符串,則StringBuilder更有效。

我閱讀了第一行,然后啟動了外部Do循環。 首先,我們使用Controls集合找到要填充的文本框。 我去掉前導括號和尾括號以獲取作為文本框名稱的字符串。

內部Do一直循環,直到我們到達文件末尾或找到下一個括號。 StringBuilder上調用.ToString並將其添加到文本框中。 StringBuilder被清除,我們將 go 到外部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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM