1

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

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
4

2 回答 2

1

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

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

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

.. 应该:

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

此代码将起作用:

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

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

于 2012-10-21T23:49:52.473 回答
1

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

RTF 文件的代码:

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

TXT 文件的代码:

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

希望能帮助到你

-nfell2009

于 2013-11-23T14:11:39.067 回答