更新:2007 年 11 月
此示例加载用户在 OpenFileDialog 中选择的文本文件。然后代码用此文件的内容填充 RichTextBox 控件。
示例
// Create an OpenFileDialog object.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the filter to look for text files.
openFile1.Filter = "Text Files|*.txt";
// If the user selected a file, load its contents into the RichTextBox.
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox1.LoadFile(openFile1.FileName,
RichTextBoxStreamType.PlainText);
编译代码
此示例要求:
- 一个名为 richTextBox1 的 RichTextBox 控件。将此代码段插入到 Form1_Load 方法中。程序运行时,将提示您选择文本文件。