How to: Load Text into a RichTextBox Control
This example loads a text file that a user selects in the OpenFileDialog. The code then populates a RichTextBox control with the file's contents.
Example
// 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);
Compiling the Code
This example requires:
- A RichTextBox control named richTextBox1. Insert the code segment into the Form1_Load method. When you run the program, you will be prompted to select a text file.
See Also
Concepts
Designing a User Interface in Visual C#