如何使用txt按行填充富文本框行? VB

时间:2012-03-04 12:18:52

标签: vb.net richtextbox

我正在尝试使用txt文件逐行填充富文本框...没有结果!

这是想要获得的结果:

  1. House 2. Dog 3. Cat 4. Etc
  2. 每一行都在另一行

    之下

    任何帮助都会很棒! 提前谢谢你!

2 个答案:

答案 0 :(得分:3)

这是非常简单的兄弟:

Private Sub FillRichTextBoxFromFile(ByVal path As String)
        Try
            If IO.File.Exists(path) Then
                Using sr As New IO.StreamReader(path)
                    Dim s As String = ""
                    Dim i As Integer = 1
                    While Not sr.EndOfStream
                        s += CStr(i) + ". " + sr.ReadLine + vbNewLine
                        i += 1
                    End While
                    RichTextBox1.Text = s
                End Using
            Else
                MsgBox("Oooops, File not found !!!")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

答案 1 :(得分:1)

你也可以试试这个

    Dim fileContents() As String

    If Not My.Computer.FileSystem.FileExists("C:\New.txt") Then

        MsgBox("File Not Found")
        Exit Sub

    End If

    RichTextBox1.Text = vbNullString

    fileContents = Split(My.Computer.FileSystem.ReadAllText("C:\New.txt"), vbNewLine)

    For i = 0 To fileContents.Count - 1

        RichTextBox1.Text += i + 1 & ". " & fileContents(i) & vbNewLine

    Next