简体   繁体   English

如何用txt逐行填充RTF文本框? VB

[英]How to populate Rich Text Box Line By line with txt? vb

I'm trying to populate a rich text box line by line with txt file... without result! 我正在尝试用txt文件逐行填充富文本框...没有结果!

This is the result that would like to get: 这是想要得到的结果:

  1. House 2. Dog 3. Cat 4. Etc 房屋2.狗3.猫4.等

each one in a row below the other 每一个都在另一个下面

Any help would be great! 任何帮助将是巨大的! thank you in advance! 先感谢您!

It's quite simple brother : 很简单的兄弟:

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

You could also try this 您也可以尝试

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM