-1

I have spent hours trying to fix this problem I have, basically I want to only display column 1 from my text file in my rich text box.

Each column is seperated by a space (" ")

When I run the following coding it shows all columns.

RichTextBox1.Text = System.IO.File.ReadAllText("Path")
    Dim str1() As String = Nothing
    Dim LinesList2 As New List(Of String)

    For Each line1 In LinesList2
        str1 = line1.Split(" "c)
        If str1(0) = line1 Then
            Dim Hold As String = Nothing
            Hold = line1 & " " & str1(1).ToString
            LinesList2.Add(Hold)
        End If
    Next
    LinesList2.Sort()

    For Each Str As String In LinesList2
        RichTextBox1.AppendText(Str & Environment.NewLine)
    Next
4

4 回答 4

2

使用 LINQ 会更容易:

Dim firstColumnText = From line In System.IO.File.ReadLines(path)
                      Select line.Split(" "c)(0)
RichTextBox1.Text = String.Join(Environment.NewLine, firstColumnText)

除此之外,您在这里使用的是一个空列表:

' reads the whole file and set it as text for the RichTextBox '
RichTextBox1.Text = System.IO.File.ReadAllText(path)
' creates an empty List(Of String) '
Dim LinesList2 As New List(Of String)
' "Iterates" the empty list '
For Each line1 In LinesList2
    '  .....
于 2012-05-25T23:06:03.767 回答
1

You're not reading anything into LinesList2, so nothing inside either loop gets executed. Even if you did, the line Hold = line1 & ... seems to include the whole line.

于 2012-05-25T23:03:52.457 回答
0

您正在添加整行

    Hold = line1 & " " & str1(1).ToString
    LinesList2.Add(Hold)

目的是什么?

str1(0) = line1

这只有在没有分裂的情况下才会成立。

您需要调试 line1 和 sr1()

于 2012-05-25T23:04:00.460 回答
0

除非您缺少代码,否则您已经创建LinesList2但没有填充任何内容,因此没有什么可循环的。

于 2012-05-25T23:04:31.320 回答