--- title: Load Files into RichTextBox Control ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" - "cpp" helpviewer_keywords: - "text boxes [Windows Forms], displaying files" - "examples [Windows Forms], text boxes" - ".rtf files [Windows Forms], opening in RichTextBox control" - "RTF files [Windows Forms], opening in RichTextBox control" - "text files [Windows Forms], displaying in RichTextBox control" - ".rtf files [Windows Forms], displaying in RichTextBox control" - "RichTextBox control [Windows Forms], opening files" - "RTF files [Windows Forms], displaying in RichTextBox control" ms.assetid: c03451be-f285-4428-a71a-c41e002cc919 --- # How to: Load Files into the Windows Forms RichTextBox Control The Windows Forms control can display a plain-text, Unicode plain-text, or Rich-Text-Format (RTF) file. To do so, call the method. You can also use the method to load data from a stream. For more information, see . ### To load a file into the RichTextBox control 1. Determine the path of the file to be opened using the component. For an overview, see [OpenFileDialog Component Overview](openfiledialog-component-overview-windows-forms.md). 2. Call the method of the control, specifying the file to load and optionally a file type. In the example below, the file to load is taken from the component's property. If you call the method with a file name as its only argument, the file type will be assumed to be RTF. To specify another file type, call the method with a value of the enumeration as its second argument. In the example below, the component is shown when a button is clicked. The file selected is then opened and displayed in the control. This example assumes a form has a button,`btnOpenFile`. ```vb Private Sub btnOpenFile_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOpenFile.Click If OpenFileDialog1.ShowDialog() = DialogResult.OK Then RichTextBox1.LoadFile(OpenFileDialog1.FileName, _ RichTextBoxStreamType.RichText) End If End Sub ``` ```csharp private void btnOpenFile_Click(object sender, System.EventArgs e) { if(openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText); } } ``` ```cpp private: void btnOpenFile_Click(System::Object ^ sender, System::EventArgs ^ e) { if(openFileDialog1->ShowDialog() == DialogResult::OK) { richTextBox1->LoadFile(openFileDialog1->FileName, RichTextBoxStreamType::RichText); } } ``` (Visual C#, Visual C++) Place the following code in the form's constructor to register the event handler. ```csharp this.btnOpenFile.Click += new System.EventHandler(this. btnOpenFile_Click); ``` ```cpp this->btnOpenFile->Click += gcnew System::EventHandler(this, &Form1::btnOpenFile_Click); ``` > [!IMPORTANT] > To run this process, your assembly may require a privilege level granted by the class. If you are running in a partial-trust context, the process might throw an exception because of insufficient privileges. For more information, see [Code Access Security Basics](https://docs.microsoft.com/dotnet/framework/misc/code-access-security-basics). ## See also - - - [RichTextBox Control](richtextbox-control-windows-forms.md) - [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)