RichTextBox.LoadFile RichTextBox.LoadFile RichTextBox.LoadFile RichTextBox.LoadFile Method

Définition

Charge le contenu d'un fichier dans le contrôle RichTextBox.Loads the contents of a file into the RichTextBox control.

Surcharges

LoadFile(String) LoadFile(String) LoadFile(String) LoadFile(String)

Charge un fichier RTF ou un fichier texte ASCII standard dans le contrôle RichTextBox.Loads a rich text format (RTF) or standard ASCII text file into the RichTextBox control.

LoadFile(Stream, RichTextBoxStreamType) LoadFile(Stream, RichTextBoxStreamType) LoadFile(Stream, RichTextBoxStreamType) LoadFile(Stream, RichTextBoxStreamType)

Charge le contenu d'un flux de données existant dans le contrôle RichTextBox.Loads the contents of an existing data stream into the RichTextBox control.

LoadFile(String, RichTextBoxStreamType) LoadFile(String, RichTextBoxStreamType) LoadFile(String, RichTextBoxStreamType) LoadFile(String, RichTextBoxStreamType)

Charge un type de fichier spécifique dans le contrôle RichTextBox.Loads a specific type of file into the RichTextBox control.

LoadFile(String) LoadFile(String) LoadFile(String) LoadFile(String)

Charge un fichier RTF ou un fichier texte ASCII standard dans le contrôle RichTextBox.Loads a rich text format (RTF) or standard ASCII text file into the RichTextBox control.

public:
 void LoadFile(System::String ^ path);
public void LoadFile (string path);
member this.LoadFile : string -> unit
Public Sub LoadFile (path As String)

Paramètres

path
String String String String

Nom et emplacement du fichier à charger dans le contrôle.The name and location of the file to load into the control.

Exceptions

Une erreur s'est produite lors du chargement du fichier dans le contrôle.An error occurred while loading the file into the control.

Le fichier chargé n'est pas un document RTF.The file being loaded is not an RTF document.

Exemples

L’exemple de code suivant ouvre un fichier RTF dans RichTextBox le contrôle.The following code example opens an RTF file into the RichTextBox control. L’exemple utilise la OpenFileDialog classe pour afficher une boîte de dialogue qui demande le fichier à l’utilisateur.The example uses the OpenFileDialog class to display a dialog to request the file from the user. Le code charge ensuite ce fichier en supposant qu’il s’agit d’un fichier de document RTF.The code then loads that file assuming that it is an RTF document file. Si le fichier n’est pas, l’exemple de code lèvera une exception.If the file is not, the example code will throw an exception. Cet exemple requiert que le code soit placé dans une Form classe qui a un RichTextBox contrôle nommé richTextBox1.This example requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1.

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 );
      }
   }
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);
   }
}

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)
    End If
End Sub

Remarques

Lors du chargement d’un fichier LoadFile avec la méthode, le contenu du fichier en cours de chargement remplace tout le RichTextBox contenu du contrôle.When loading a file with the LoadFile method, the contents of the file being loaded replace the entire contents of the RichTextBox control. Les valeurs des Text propriétés et Rtf sont alors modifiées.This will cause the values of the Text and Rtf properties to change. Vous pouvez utiliser cette méthode pour charger un document texte ou RTF créé précédemment dans le contrôle pour la manipulation.You can use this method to load a previously created text or RTF document into the control for manipulation. Si vous souhaitez enregistrer le fichier, vous pouvez utiliser la SaveFile méthode.If you want to save the file, you can use the SaveFile method.

Notes

Avec cette version de la LoadFile méthode, si le fichier chargé n’est pas un document RTF, une exception se produit.With this version of the LoadFile method, if the file being loaded is not an RTF document, an exception will occur. Pour charger un type de fichier différent, tel qu’un fichier texte ASCII, utilisez les autres versions de cette méthode qui acceptent une valeur de RichTextBoxStreamType l’énumération en tant que paramètre.To load a different type of file such as an ASCII text file, use the other versions of this method that accept a value from the RichTextBoxStreamType enumeration as a parameter.

Notes

La LoadFile méthode n’ouvre pas un fichier tant qu’un handle n’a pas RichTextBoxété créé pour le.The LoadFile method will not open a file until a handle is created for the RichTextBox. Assurez-vous que le handle du contrôle est créé LoadFile avant d’appeler la méthode.Ensure that the control's handle is created before calling the LoadFile method.

Sécurité

FileIOPermission
pour ouvrir un fichier.for opening a file. Énumération associée : Read Valeur deFileIOPermissionAccess.Associated enumeration: The Read value of FileIOPermissionAccess.

Voir aussi

LoadFile(Stream, RichTextBoxStreamType) LoadFile(Stream, RichTextBoxStreamType) LoadFile(Stream, RichTextBoxStreamType) LoadFile(Stream, RichTextBoxStreamType)

Charge le contenu d'un flux de données existant dans le contrôle RichTextBox.Loads the contents of an existing data stream into the RichTextBox control.

public:
 void LoadFile(System::IO::Stream ^ data, System::Windows::Forms::RichTextBoxStreamType fileType);
public void LoadFile (System.IO.Stream data, System.Windows.Forms.RichTextBoxStreamType fileType);
member this.LoadFile : System.IO.Stream * System.Windows.Forms.RichTextBoxStreamType -> unit
Public Sub LoadFile (data As Stream, fileType As RichTextBoxStreamType)

Paramètres

data
Stream Stream Stream Stream

Flux de données à charger dans le contrôle RichTextBox.A stream of data to load into the RichTextBox control.

Exceptions

Une erreur s'est produite lors du chargement du fichier dans le contrôle.An error occurred while loading the file into the control.

Le fichier chargé n'est pas un document RTF.The file being loaded is not an RTF document.

Exemples

L’exemple de code suivant illustre l' SaveFile utilisation LoadFile des méthodes et avec des flux.The following code example demonstrates using the SaveFile and LoadFile methods with streams. Il illustre également l’utilisation FileDialog.FileNamedes FileDialog.DefaultExtmembres SaveFileDialog.CreatePrompt,, SaveFileDialog.OverwritePrompt et.It also demonstrates using the FileDialog.FileName, FileDialog.DefaultExt, SaveFileDialog.CreatePrompt, and SaveFileDialog.OverwritePrompt members.

Il s’agit d’un exemple complet qui est prêt à s’exécuter lorsque vous le copiez dans votre projet.This is a complete example that is ready to run when you copy it to your project.

using namespace System;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Windows::Forms;

public ref class Form1: public Form
{
public private:
   RichTextBox^ RichTextBox1;
   Button^ Button1;
   RichTextBox^ RichTextBox2;
   Button^ Button2;
   SaveFileDialog^ SaveFileDialog1;

public:
   Form1()
      : Form()
   {
      userInput = gcnew MemoryStream;
      this->RichTextBox1 = gcnew RichTextBox;
      this->Button1 = gcnew Button;
      this->RichTextBox2 = gcnew RichTextBox;
      this->Button2 = gcnew Button;
      this->SaveFileDialog1 = gcnew SaveFileDialog;
      this->SuspendLayout();
      this->RichTextBox1->Location = Point( 24, 64 );
      this->RichTextBox1->Name = "RichTextBox1";
      this->RichTextBox1->TabIndex = 0;
      this->RichTextBox1->Text = "Type something here.";
      this->Button1->Location = Point( 96, 16 );
      this->Button1->Name = "Button1";
      this->Button1->Size = Size( 96, 24 );
      this->Button1->TabIndex = 1;
      this->Button1->Text = "Save To Stream";
      this->Button1->Click += 
          gcnew EventHandler( this, &Form1::Button1_Click );
      this->RichTextBox2->Location = Point( 152, 64 );
      this->RichTextBox2->Name = "RichTextBox2";
      this->RichTextBox2->TabIndex = 3;
      this->RichTextBox2->Text = "It will be added to the stream "
      "and appear here.";
      this->Button2->Location = Point( 104, 200 );
      this->Button2->Name = "Button2";
      this->Button2->Size = Size( 88, 32 );
      this->Button2->TabIndex = 4;
      this->Button2->Text = "Save Stream To File";
      this->Button2->Click += 
          gcnew EventHandler( this, &Form1::Button2_Click );
      this->ClientSize = Size( 292, 266 );
      this->Controls->Add( this->Button2 );
      this->Controls->Add( this->RichTextBox2 );
      this->Controls->Add( this->Button1 );
      this->Controls->Add( this->RichTextBox1 );
      this->Name = "Form1";
      this->Text = "Form1";
      this->ResumeLayout( false );
   }

   // Declare a new memory stream.
   MemoryStream^ userInput;

private:

   // Save the content of RichTextBox1 to the memory stream, 
   // appending a LineFeed character.  
   void Button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      RichTextBox1->SaveFile( userInput, RichTextBoxStreamType::PlainText );
      userInput->WriteByte( 13 );
      
      // Display the entire contents of the stream,
      // by setting its position to 0, to RichTextBox2.
      userInput->Position = 0;
      RichTextBox2->LoadFile( userInput, RichTextBoxStreamType::PlainText );
   }


   // Shows the use of a SaveFileDialog to save a MemoryStream to a file.
   void Button2_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      
      // Set the properties on SaveFileDialog1 so the user is 
      // prompted to create the file if it doesn't exist 
      // or overwrite the file if it does exist.
      SaveFileDialog1->CreatePrompt = true;
      SaveFileDialog1->OverwritePrompt = true;
      
      // Set the file name to myText.txt, set the type filter
      // to text files, and set the initial directory to the
      // MyDocuments folder.
      SaveFileDialog1->FileName = "myText";
      // DefaultExt is only used when "All files" is selected from 
      // the filter box and no extension is specified by the user.
      SaveFileDialog1->DefaultExt = "txt";
      SaveFileDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
      SaveFileDialog1->InitialDirectory = 
          Environment->GetFolderPath(Environment::SpecialFolder::MyDocuments);
      
      // Call ShowDialog and check for a return value of DialogResult.OK,
      // which indicates that the file was saved. 
      DialogResult result = SaveFileDialog1->ShowDialog();
      Stream^ fileStream;
      if ( result == DialogResult::OK )
      {
         fileStream = SaveFileDialog1->OpenFile();
         userInput->Position = 0;
         userInput->WriteTo( fileStream );
         fileStream->Close();
      }
   }
};

int main()
{
   Application::Run( gcnew Form1 );
}

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

public partial class Form1: Form
{
    internal RichTextBox RichTextBox1;
    internal Button Button1;
    internal RichTextBox RichTextBox2;
    internal Button Button2;
    internal SaveFileDialog SaveFileDialog1;

    public Form1() : base()
    {   
        this.RichTextBox1 = new RichTextBox();
        this.Button1 = new Button();
        this.RichTextBox2 = new RichTextBox();
        this.Button2 = new Button();
        this.SaveFileDialog1 = new SaveFileDialog();
        this.SuspendLayout();
        this.RichTextBox1.Location = new Point(24, 64);
        this.RichTextBox1.Name = "RichTextBox1";
        this.RichTextBox1.TabIndex = 0;
        this.RichTextBox1.Text = "Type something here.";
        this.Button1.Location = new Point(96, 16);
        this.Button1.Name = "Button1";
        this.Button1.Size = new Size(96, 24);
        this.Button1.TabIndex = 1;
        this.Button1.Text = "Save To Stream";
        this.Button1.Click += new EventHandler(Button1_Click);
        this.RichTextBox2.Location = new Point(152, 64);
        this.RichTextBox2.Name = "RichTextBox2";
        this.RichTextBox2.TabIndex = 3;
        this.RichTextBox2.Text = 
            "It will be added to the stream and appear here.";
        this.Button2.Location = new Point(104, 200);
        this.Button2.Name = "Button2";
        this.Button2.Size = new Size(88, 32);
        this.Button2.TabIndex = 4;
        this.Button2.Text = "Save Stream To File";
        this.Button2.Click += new EventHandler(Button2_Click);
        this.ClientSize = new Size(292, 266);
        this.Controls.Add(this.Button2);
        this.Controls.Add(this.RichTextBox2);
        this.Controls.Add(this.Button1);
        this.Controls.Add(this.RichTextBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }

    // Declare a new memory stream.
    MemoryStream userInput = new MemoryStream();

    // Save the content of RichTextBox1 to the memory stream, 
    // appending a LineFeed character.  
    private void Button1_Click(Object sender, EventArgs e)
    {
        RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText);
        userInput.WriteByte(13);

        // Display the entire contents of the stream,
        // by setting its position to 0, to RichTextBox2.
        userInput.Position = 0;
        RichTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText);
    }

    // Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    private void Button2_Click(Object sender, EventArgs e)
    {
        // Set the properties on SaveFileDialog1 so the user is 
        // prompted to create the file if it doesn't exist 
        // or overwrite the file if it does exist.
        SaveFileDialog1.CreatePrompt = true;
        SaveFileDialog1.OverwritePrompt = true;

        // Set the file name to myText.txt, set the type filter
        // to text files, and set the initial directory to the 
        // MyDocuments folder.
        SaveFileDialog1.FileName = "myText";
        // DefaultExt is only used when "All files" is selected from 
        // the filter box and no extension is specified by the user.
        SaveFileDialog1.DefaultExt = "txt";
        SaveFileDialog1.Filter = 
            "Text files (*.txt)|*.txt|All files (*.*)|*.*";
        SaveFileDialog1.InitialDirectory = 
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        // Call ShowDialog and check for a return value of DialogResult.OK,
        // which indicates that the file was saved. 
        DialogResult result = SaveFileDialog1.ShowDialog();
        Stream fileStream;

        if (result == DialogResult.OK)
        {
            // Open the file, copy the contents of memoryStream to fileStream,
            // and close fileStream. Set the memoryStream.Position value to 0 
            // to copy the entire stream. 
            fileStream = SaveFileDialog1.OpenFile();
            userInput.Position = 0;
            userInput.WriteTo(fileStream);
            fileStream.Close();
        }
    }
}
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

Partial Public Class Form1
    Inherits Form

    Friend WithEvents RichTextBox1 As RichTextBox
    Friend WithEvents Button1 As Button
    Friend WithEvents RichTextBox2 As RichTextBox
    Friend WithEvents Button2 As Button
    Friend WithEvents SaveFileDialog1 As SaveFileDialog

    Public Sub New()
        MyBase.New()
        Me.RichTextBox1 = New RichTextBox
        Me.Button1 = New Button
        Me.RichTextBox2 = New RichTextBox
        Me.Button2 = New Button
        Me.SaveFileDialog1 = New SaveFileDialog
        Me.SuspendLayout()
        Me.RichTextBox1.Location = New Point(24, 64)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = "Type something here."
        Me.Button1.Location = New Point(96, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Save To Stream"
        Me.RichTextBox2.Location = New Point(152, 64)
        Me.RichTextBox2.Name = "RichTextBox2"
        Me.RichTextBox2.TabIndex = 3
        Me.RichTextBox2.Text = "It will be added to the stream and appear here."
        Me.Button2.Location = New Point(104, 200)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New Size(88, 32)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Save Stream To File"
        Me.ClientSize = New Size(292, 266)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.RichTextBox2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.RichTextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub

    ' Declare a new memory stream.
    Dim userInput As New MemoryStream

    ' Save the content of RichTextBox1 to the memory stream, appending
    'a LineFeed character.  
    Private Sub Button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button1.Click
        RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText)
        userInput.WriteByte(13)

        ' Display the entire contents of the stream,
        ' by setting its position to 0, to RichTextBox2.
        userInput.Position = 0
        RichTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText)
    End Sub

    ' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    Private Sub Button2_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button2.Click

        ' Set the properties on SaveFileDialog1 so the user is 
        ' prompted to create the file if it doesn't exist 
        ' or overwrite the file if it does exist.
        SaveFileDialog1.CreatePrompt = True
        SaveFileDialog1.OverwritePrompt = True

        ' Set the file name to myText.txt, set the type filter
        ' to text files, and set the initial directory to the 
        ' MyDocuments folder.
        SaveFileDialog1.FileName = "myText"
        ' DefaultExt is only used when "All files" is selected from 
        ' the filter box and no extension is specified by the user.
        SaveFileDialog1.DefaultExt = "txt"
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        SaveFileDialog1.InitialDirectory = _
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

        ' Call ShowDialog and check for a return value of DialogResult.OK,
        ' which indicates that the file was saved. 
        Dim result As DialogResult = SaveFileDialog1.ShowDialog()
        Dim fileStream As Stream

        If (result = DialogResult.OK) Then
            ' Open the file, copy the contents of memoryStream to fileStream,
            ' and close fileStream. Set the memoryStream.Position value to 0 to 
            ' copy the entire stream. 
            fileStream = SaveFileDialog1.OpenFile()
            userInput.Position = 0
            userInput.WriteTo(fileStream)
            fileStream.Close()
        End If
    End Sub

End Class

Remarques

Vous pouvez utiliser cette version de la LoadFile méthode pour charger le RichTextBox avec les données d’un flux de données existant.You can use this version of the LoadFile method to load the RichTextBox with data from an existing stream of data. Les données chargées dans le contrôle remplacent tout le contenu du RichTextBox contrôle.The data that is loaded into the control replaces the entire contents of the RichTextBox control. Les valeurs des Text propriétés et Rtf sont alors modifiées.This will cause the values of the Text and Rtf properties to change. Vous pouvez utiliser cette méthode pour charger un fichier qui a été précédemment ouvert dans un flux de données dans le contrôle pour la manipulation.You can use this method to load a file that has been previously opened into a data stream into the control for manipulation. Si vous souhaitez enregistrer le contenu du contrôle dans le flux, vous pouvez utiliser la SaveFile méthode qui accepte un Stream objet comme paramètre.If you want to save contents of the control back into the stream, you can use the SaveFile method that accepts a Stream object as a parameter.

Cette version de la LoadFile méthode vous permet également de spécifier le type de données chargées dans le contrôle.This version of the LoadFile method also enables you to specify the type of data that is being loaded into the control. Cette fonctionnalité vous permet d’utiliser des flux de données qui contiennent des données autres que des documents au format RTF (Rich Text Format) dans le contrôle.This feature enables you to use data streams that contain data other than rich text format (RTF) documents into the control.

Notes

La LoadFile méthode n’ouvre pas un fichier tant qu’un handle n’a pas RichTextBoxété créé pour le.The LoadFile method will not open a file until a handle is created for the RichTextBox. Assurez-vous que le handle du contrôle est créé LoadFile avant d’appeler la méthode.Ensure that the control's handle is created before calling the LoadFile method.

Voir aussi

LoadFile(String, RichTextBoxStreamType) LoadFile(String, RichTextBoxStreamType) LoadFile(String, RichTextBoxStreamType) LoadFile(String, RichTextBoxStreamType)

Charge un type de fichier spécifique dans le contrôle RichTextBox.Loads a specific type of file into the RichTextBox control.

public:
 void LoadFile(System::String ^ path, System::Windows::Forms::RichTextBoxStreamType fileType);
public void LoadFile (string path, System.Windows.Forms.RichTextBoxStreamType fileType);
member this.LoadFile : string * System.Windows.Forms.RichTextBoxStreamType -> unit
Public Sub LoadFile (path As String, fileType As RichTextBoxStreamType)

Paramètres

path
String String String String

Nom et emplacement du fichier à charger dans le contrôle.The name and location of the file to load into the control.

Exceptions

Une erreur s'est produite lors du chargement du fichier dans le contrôle.An error occurred while loading the file into the control.

Le fichier chargé n'est pas un document RTF.The file being loaded is not an RTF document.

Exemples

L’exemple de code suivant ouvre un fichier texte dans RichTextBox le contrôle.The following code example opens a text file into the RichTextBox control. L’exemple utilise la OpenFileDialog classe pour afficher une boîte de dialogue qui demande le fichier à l’utilisateur.The example uses the OpenFileDialog class to display a dialog to request the file from the user. Le code charge ensuite ce fichier dans le RichTextBox contrôle.The code then loads that file into the RichTextBox control. L’exemple utilise cette version de la LoadFile méthode pour spécifier que le fichier doit être ouvert en tant que fichier texte ASCII au lieu du format de texte enrichi standard.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. Cet exemple requiert que le code soit placé dans une Form classe qui a un RichTextBox contrôle nommé richTextBox1.This example requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1.

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 );
      }
   }
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);
   }
}

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

Remarques

Lors du chargement d’un fichier LoadFile avec la méthode, le contenu du fichier en cours de chargement remplace tout le RichTextBox contenu du contrôle.When loading a file with the LoadFile method, the contents of the file being loaded replace the entire contents of the RichTextBox control. Les valeurs des Text propriétés et Rtf sont alors modifiées.This will cause the values of the Text and Rtf properties to change. Vous pouvez utiliser cette méthode pour charger un document de texte ou RTF (Rich Text Format) créé précédemment dans le contrôle pour la manipulation.You can use this method to load a previously created text or rich text format (RTF) document into the control for manipulation. Si vous souhaitez enregistrer le fichier, vous pouvez utiliser la SaveFile méthode.If you want to save the file, you can use the SaveFile method.

Vous pouvez utiliser cette version de la LoadFile méthode pour spécifier le type de fichier du fichier en cours de chargement.You can use this version of the LoadFile method to specify the file type of the file being loaded. Cette fonctionnalité vous permet de charger des fichiers autres que des documents RTF dans le contrôle.This feature enables you to load files other than RTF documents into the control.

Notes

La LoadFile méthode n’ouvre pas un fichier tant qu’un handle n’a pas RichTextBoxété créé pour le.The LoadFile method will not open a file until a handle is created for the RichTextBox. Assurez-vous que le handle du contrôle est créé LoadFile avant d’appeler la méthode.Ensure that the control's handle is created before calling the LoadFile method.

Sécurité

FileIOPermission
pour ouvrir un fichier.for opening a file. Énumération associée : Read Valeur deFileIOPermissionAccess.Associated enumeration: The Read value of FileIOPermissionAccess.

Voir aussi

S’applique à