LoadFile Method

RichTextBox.LoadFile Method

Loads the contents of a file into the RichTextBox control.

Overload List

Loads a Rich Text Format (RTF) or standard ASCII text file into the RichTextBox control.

[Visual Basic] Overloads Public Sub LoadFile(String)
[C#] public void LoadFile(string);
[C++] public: void LoadFile(String*);
[JScript] public function LoadFile(String);

Loads the contents of an existing data stream into the RichTextBox control.

[Visual Basic] Overloads Public Sub LoadFile(Stream, RichTextBoxStreamType)
[C#] public void LoadFile(Stream, RichTextBoxStreamType);
[C++] public: void LoadFile(Stream*, RichTextBoxStreamType);
[JScript] public function LoadFile(Stream, RichTextBoxStreamType);

Loads a specific type of file into the RichTextBox control.

[Visual Basic] Overloads Public Sub LoadFile(String, RichTextBoxStreamType)
[C#] public void LoadFile(string, RichTextBoxStreamType);
[C++] public: void LoadFile(String*, RichTextBoxStreamType);
[JScript] public function LoadFile(String, RichTextBoxStreamType);

Example

[Visual Basic, C#, C++] The following example opens a text file into the RichTextBox control. The example uses the OpenFileDialog class to display a dialog to request the file from the user. The code then loads that file into the RichTextBox control. The example uses this version of the LoadFile method to specify that the file be opened as an ASCII text file instead of the standard Rich Text Format. This example assumes that the code is placed in a Form class that has a RichTextBox control named richTextBox1.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of LoadFile. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub LoadMyFile()
    ' Create an OpenFileDialog to request a file to open.
    Dim openFile1 As New OpenFileDialog()
    
    ' Initialize the OpenFileDialog to look for RTF files.
    openFile1.DefaultExt = "*.rtf"
    openFile1.Filter = "RTF Files|*.rtf"
    
    ' Determine whether the user selected a file from the OpenFileDialog.
    If (openFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
        And (openFile1.FileName.Length > 0) Then
        
        ' Load the contents of the file into the RichTextBox.
        richTextBox1.LoadFile(openFile1.FileName, _
            RichTextBoxStreamType.PlainText)
    End If
End Sub


[C#] 
public void LoadMyFile()
{
   // Create an OpenFileDialog to request a file to open.
   OpenFileDialog openFile1 = new OpenFileDialog();

   // Initialize the OpenFileDialog to look for RTF files.
   openFile1.DefaultExt = "*.rtf";
   openFile1.Filter = "RTF Files|*.rtf";

   // Determine whether the user selected a file from the OpenFileDialog.
   if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
      openFile1.FileName.Length > 0) 
   {
      // Load the contents of the file into the RichTextBox.
      richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
   }
}


[C++] 
public:
void LoadMyFile()
{
   // Create an OpenFileDialog to request a file to open.
   OpenFileDialog* openFile1 = new OpenFileDialog();

   // Initialize the OpenFileDialog to look for RTF files.
   openFile1->DefaultExt = S"*.rtf";
   openFile1->Filter = S"RTF Files|*.rtf";

   // Determine whether the user selected a file from the OpenFileDialog.
   if(openFile1->ShowDialog() == System::Windows::Forms::DialogResult::OK &&
      openFile1->FileName->Length > 0) 
   {
      // Load the contents of the file into the RichTextBox.
      richTextBox1->LoadFile(openFile1->FileName, RichTextBoxStreamType::PlainText);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

RichTextBox Class | RichTextBox Members | System.Windows.Forms Namespace

Show:
© 2016 Microsoft