次の方法で共有


RichTextBox.LoadFile メソッド

ファイルの内容を RichTextBox コントロールに読み込みます。

オーバーロードの一覧

RTF (Rich Text Format) ファイルまたは標準の ASCII テキスト ファイルを RichTextBox コントロールに読み込みます。

[Visual Basic] Overloads Public Sub LoadFile(String)

[C#] public void LoadFile(string);

[C++] public: void LoadFile(String*);

[JScript] public function LoadFile(String);

既存のデータ ストリームの内容を RichTextBox コントロールに読み込みます。

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

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

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

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、LoadFile のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間