更新:2007 年 11 月
這個範例會載入使用者在 OpenFileDialog 中所選取的文字檔。程式碼接著會以檔案的內容填入 (Populate) 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 方法。執行程式時,將會提示您選取文字檔。