如何使用 vb.net 在富文本框中打开文件并查看?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13003035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:00:21  来源:igfitidea点击:

How to op-en file and view in a rich text box using vb.net?

vb.net

提问by Aaron Warnke

Hi I am trying to open and view a files text in a rich text box. Here is what I have please let me know what I am doing wrong?

嗨,我正在尝试在富文本框中打开和查看文件文本。这是我所拥有的,请让我知道我做错了什么?

Private Sub loadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadButton.Click

    ' Displays an OpenFileDialog so the user can select a Cursor.
    Dim openFileDialog1 As New OpenFileDialog()
    openFileDialog1.Filter = "Cursor Files|*.txt"
    openFileDialog1.Title = "Select a Cursor File"

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        ' Assign the cursor in the Stream to the Form's Cursor property.
        Me.mainRTBox = New Text(openFileDialog1.OpenFile())
    End If

End Sub

采纳答案by Arrow

The problem you were having was that you weren't reading the file at all, and you weren't assigning the content of the file to the RichTextBox correctly.

您遇到的问题是您根本没有读取文件,并且您没有正确地将文件内容分配给 RichTextBox。

Specifically, this code you have:

具体来说,你有这个代码:

Me.mainRTBox = New Text(openFileDialog1.OpenFile())

Me.mainRTBox = New Text(openFileDialog1.OpenFile())

.. should be:

.. 应该:

Me.mainRTBox.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)

Me.mainRTBox.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)

This code will work:

此代码将起作用:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Displays an OpenFileDialog so the user can select a Cursor.
        Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "Cursor Files|*.cur"
        openFileDialog1.Title = "Select a Cursor File"

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            ' Assign the cursor in the Stream to the Form's Cursor property.

            Dim extension = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("."))

            If extension Is "cur" Then
                Me.mainRTBox.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)
            End If

        End If
    End Sub
End Class

Edit: I updated the code so that it checks if the user did actually open a Cur (cursor) file.

编辑:我更新了代码,以便它检查用户是否确实打开了 Cur(光标)文件。

回答by BaeFell

RichTextBoxes have a built in function for viewing RTF files and TXT files.

RichTextBoxes 具有用于查看 RTF 文件和 TXT 文件的内置功能。

Code for a RTF file:

RTF 文件的代码:

RichTextBox1.LoadFile("YOUR DIRECTORY", RichTextBoxStreamType.RichText)

Code for a TXT file:

TXT 文件的代码:

RichTextBox1.LoadFile("YOUR DIRECTORY", RichTextBoxStreamType.PlainText)

Hope it helps

希望能帮助到你

-nfell2009

-nfell2009