LoadFile Method (String, RichTextBoxStreamType)
.NET Framework Class Library
RichTextBox.LoadFile Method (String, RichTextBoxStreamType)

Loads a specific type of file into the RichTextBox control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Visual Basic (Declaration)
Public Sub LoadFile ( _
    path As String, _
    fileType As RichTextBoxStreamType _
)
Visual Basic (Usage)
Dim instance As RichTextBox
Dim path As String
Dim fileType As RichTextBoxStreamType

instance.LoadFile(path, fileType)
C#
public void LoadFile (
    string path,
    RichTextBoxStreamType fileType
)
C++
public:
void LoadFile (
    String^ path, 
    RichTextBoxStreamType fileType
)
J#
public void LoadFile (
    String path, 
    RichTextBoxStreamType fileType
)
JScript
public function LoadFile (
    path : String, 
    fileType : RichTextBoxStreamType
)

Parameters

path

The name and location of the file to load into the control.

fileType

One of the RichTextBoxStreamType values.

Exception typeCondition

IOException

An error occurred while loading the file into the control.

ArgumentException

The file being loaded is not an RTF document.

When loading a file with the LoadFile method, the contents of the file being loaded replace the entire contents of the RichTextBox control. This will cause the values of the Text and Rtf properties to change. You can use this method to load a previously created text or rich text format (RTF) document into the control for manipulation. If you want to save the file, you can use the SaveFile method.

You can use this version of the LoadFile method to specify the file type of the file being loaded. This feature enables you to load files other than RTF documents into the control.

NoteNote

The LoadFile method will not open a file until a handle is created for the RichTextBox. Ensure that the control's handle is created before calling the LoadFile method.

The following code 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 requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1.

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 = gcnew 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 );
      }
   }
J#
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.set_DefaultExt("*.rtf");
    openFile1.set_Filter("RTF Files|*.rtf");

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker