Skip to content

Latest commit

 

History

History
84 lines (70 loc) · 4.4 KB

how-to-load-files-into-the-windows-forms-richtextbox-control.md

File metadata and controls

84 lines (70 loc) · 4.4 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
Load Files into RichTextBox Control
Learn how to load files into RichTextBox control to display a plain-text, Unicode plain-text, or rich-text-format (RTF) file.
03/30/2017
csharp
vb
cpp
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
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

  1. Determine the path of the file to be opened using the xref:System.Windows.Forms.OpenFileDialog component. For an overview, see OpenFileDialog Component Overview.

  2. 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 Sub
    private 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.

See also