4.3 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Load Files into RichTextBox Control | 03/30/2017 |
|
|
c03451be-f285-4428-a71a-c41e002cc919 |
How to: Load Files into the Windows Forms RichTextBox Control
The Windows Forms xref:System.Windows.Forms.RichTextBox control can display a plain-text, Unicode plain-text, or Rich-Text-Format (RTF) file. To do so, call the xref:System.Windows.Forms.RichTextBox.LoadFile%2A method. You can also use the xref:System.Windows.Forms.RichTextBox.LoadFile%2A method to load data from a stream. For more information, see xref:System.Windows.Forms.RichTextBox.LoadFile%28System.IO.Stream%2CSystem.Windows.Forms.RichTextBoxStreamType%29.
To load a file into the RichTextBox control
-
Determine the path of the file to be opened using the xref:System.Windows.Forms.OpenFileDialog component. For an overview, see OpenFileDialog Component Overview.
-
Call the xref:System.Windows.Forms.RichTextBox.LoadFile%2A method of the xref:System.Windows.Forms.RichTextBox control, specifying the file to load and optionally a file type. In the example below, the file to load is taken from the xref:System.Windows.Forms.OpenFileDialog component's xref:System.Windows.Forms.FileDialog.FileName%2A 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 xref:System.Windows.Forms.RichTextBoxStreamType enumeration as its second argument.
In the example below, the xref:System.Windows.Forms.OpenFileDialog component is shown when a button is clicked. The file selected is then opened and displayed in the xref:System.Windows.Forms.RichTextBox control. This example assumes a form has a button,
btnOpenFile.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 Subprivate void btnOpenFile_Click(object sender, System.EventArgs e) { if(openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText); } }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.
this.btnOpenFile.Click += new System.EventHandler(this. btnOpenFile_Click);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 xref:System.Security.Permissions.FileIOPermission?displayProperty=nameWithType 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.