次の方法で共有


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

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

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

パラメータ

例外

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

解説

このバージョンの LoadFile メソッドを使用して、既存のデータ ストリームのデータを RichTextBox に読み込むことができます。 RichTextBox コントロールのすべての内容は、コントロールに読み込まれるデータで置換されます。この結果、 Text プロパティと Rtf プロパティの値が変更されます。このメソッドを使用して、以前開かれ、データ ストリームに書き込まれたファイルをコントロールに読み込んで処理できます。コントロールの内容をストリームに保存し直す場合は、 Stream オブジェクトをパラメータとして受け入れる SaveFile メソッドを使用できます。

このバージョンの LoadFile メソッドを使用すると、コントロールに読み込むデータの種類を指定できます。この機能により、RTF (Rich Text Format) ドキュメント以外のデータが含まれているデータ ストリームをコントロールに読み込むことができます。

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

使用例

[Visual Basic, C#] SaveFile メソッドと LoadFile メソッドをストリームと一緒に使用するコード例を次に示します。また、この例では、 FileName メンバ、 CreatePrompt メンバ、 OverwritePrompt メンバ、および System.Windows.Forms.TextBox.Click メンバも使用しています。これは完成された例であるため、プロジェクトにコピーすると実行できます。

 
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents RichTextBox2 As System.Windows.Forms.RichTextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog

    Public Sub New()
        MyBase.New()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.RichTextBox2 = New System.Windows.Forms.RichTextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.SuspendLayout()
        Me.RichTextBox1.Location = New System.Drawing.Point(24, 64)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = "Type something here."
        Me.Button1.Location = New System.Drawing.Point(96, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Save To Stream"
        Me.RichTextBox2.Location = New System.Drawing.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 System.Drawing.Point(104, 200)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 32)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Save Stream To File"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.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 System.IO.MemoryStream

    ' Save the content of RichTextBox1 to the memory stream, appending
    'a LineFeed character.  
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.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 System.Object, _
        ByVal e As System.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 drive C.
        SaveFileDialog1.FileName = "myText"
        SaveFileDialog1.DefaultExt = "txt"
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
        SaveFileDialog1.InitialDirectory = "c:\"

        ' 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 System.IO.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

[C#] 
using System.Windows.Forms;



public class Form1:
    System.Windows.Forms.Form

{
    
    internal System.Windows.Forms.RichTextBox RichTextBox1;
    internal System.Windows.Forms.Button Button1;
    internal System.Windows.Forms.RichTextBox RichTextBox2;
    internal System.Windows.Forms.Button Button2;
    internal System.Windows.Forms.SaveFileDialog SaveFileDialog1;

    public Form1() : base()
    {   
        
        this.RichTextBox1 = new System.Windows.Forms.RichTextBox();
        this.Button1 = new System.Windows.Forms.Button();
        this.RichTextBox2 = new System.Windows.Forms.RichTextBox();
        this.Button2 = new System.Windows.Forms.Button();
        this.SaveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
        this.SuspendLayout();
        this.RichTextBox1.Location = new System.Drawing.Point(24, 64);
        this.RichTextBox1.Name = "RichTextBox1";
        this.RichTextBox1.TabIndex = 0;
        this.RichTextBox1.Text = "Type something here.";
        this.Button1.Location = new System.Drawing.Point(96, 16);
        this.Button1.Name = "Button1";
        this.Button1.Size = new System.Drawing.Size(96, 24);
        this.Button1.TabIndex = 1;
        this.Button1.Text = "Save To Stream";
        this.Button1.Click += new System.EventHandler(Button1_Click);
        this.RichTextBox2.Location = new System.Drawing.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 System.Drawing.Point(104, 200);
        this.Button2.Name = "Button2";
        this.Button2.Size = new System.Drawing.Size(88, 32);
        this.Button2.TabIndex = 4;
        this.Button2.Text = "Save Stream To File";
        this.Button2.Click += new System.EventHandler(Button2_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.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.
    System.IO.MemoryStream userInput = new System.IO.MemoryStream();

    // Save the content of RichTextBox1 to the memory stream, 
    // appending a LineFeed character.  
    private void Button1_Click(System.Object sender, 
        System.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(System.Object sender, System.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 drive C.
        SaveFileDialog1.FileName = "myText";
        SaveFileDialog1.DefaultExt = "txt";
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt";
        SaveFileDialog1.InitialDirectory = "c:\\";

        // Call ShowDialog and check for a return value of DialogResult.OK,
        // which indicates that the file was saved. 
        DialogResult result = SaveFileDialog1.ShowDialog();
        System.IO.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();
        }
    }
}

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

必要条件

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

参照

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