次の方法で共有


RichTextBox.LoadFile メソッド (String, RichTextBoxStreamType)

特定の種類のファイルを RichTextBox コントロールに読み込みます。

Overloads Public Sub LoadFile( _
   ByVal path As String, _   ByVal fileType As RichTextBoxStreamType _)
[C#]
public void LoadFile(stringpath,RichTextBoxStreamTypefileType);
[C++]
public: void LoadFile(String* path,RichTextBoxStreamTypefileType);
[JScript]
public function LoadFile(
   path : String,fileType : RichTextBoxStreamType);

パラメータ

  • path
    コントロールに読み込むファイルの名前と位置。
  • fileType
    RichTextBoxStreamType 値の 1 つ。

例外

例外の種類 条件
IOException コントロールへのファイルの読み込み中に発生したエラー。
ArgumentException 読み込み中のファイルは RTF ドキュメントではありません。

解説

LoadFile メソッドを使用してファイルを読み込む場合、読み込まれているファイルの内容で、 RichTextBox コントロールの内容がすべて置換されます。この結果、 Text プロパティと Rtf プロパティの値が変更されます。このメソッドを使用して、以前に作成したテキストや RTF (Rich Text Format) ドキュメントをコントロールに読み込んで処理できます。ファイルを保存する場合は、 SaveFile メソッドを使用します。

このバージョンの LoadFile メソッドを使用すると、読み込むファイルの種類を指定できます。この機能により、RTF ドキュメント以外のファイルをコントロールに読み込むことができます。

メモ    LoadFile メソッドは、 RichTextBox のハンドルが作成されるまでファイルを開きません。 LoadFile メソッドを呼び出す前に、コントロールのハンドルが作成されていることを確認してください。

使用例

[Visual Basic, C#, C++] テキスト ファイルを RichTextBox コントロール内で開く例を次に示します。この例は、 OpenFileDialog クラスを使用して、ユーザーにファイルを指定するように要求するダイアログ ボックスを表示します。次に、ユーザーが指定したファイルを RichTextBox コントロールに読み込みます。この例では、このバージョンの LoadFile メソッドを使用し、ファイルを標準の RTF ファイルではなく ASCII テキスト ファイルとして開くように指定しています。この例は、コードが richTextBox1 という名前の RichTextBox コントロールを保持する Form クラス内にあることを前提にしています。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

  • FileIOPermission (ファイルを開くために必要なアクセス許可)。 System.Security.Permissions.FileIoPermissionAccess.Read (関連する列挙体)

参照

RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間 | RichTextBox.LoadFile オーバーロードの一覧 | SaveFile | RichTextBoxStreamType