在使用VB的RichTextBox控件时加载文档(RTF)出现错误,应用程序出错,不能写入。

2025-02-24 01:04:52
推荐回答(1个)
回答1:

下面的代码示例将一个 RTF 文件打开到 RichTextBox 控件中。本示例使用 OpenFileDialog 类显示一个从用户请求文件的对话框。如果文件为 RTF 文档文件,则代码接着加载此文件。如果此文件不是 RTF 文档文件,则代码示例将引发异常。此示例要求将代码放置在包含一个名为
richTextBox1 的 RichTextBox 控件的 Form 类中。
Public Sub LoadMyFile()
' Create an OpenFileDialog to request a file to open.
Dim openFile1 As New OpenFileDialog()

' Initialize the OpenFileDialog to look for RTF files.
openFile1.DefaultExt = "*.rtf"
openFile1.Filter = "RTF Files|*.rtf"

' Determine whether the user selected a file from the OpenFileDialog.
If (openFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
And (openFile1.FileName.Length > 0) Then
' Load the contents of the file into the RichTextBox.
richTextBox1.LoadFile(openFile1.FileName)
End If
End Sub