Export (0) Print
Expand All

RichTextBox.LoadFile Method (Stream, RichTextBoxStreamType)

Loads the contents of an existing data stream into the RichTextBox control.

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

public void LoadFile (
	Stream data,
	RichTextBoxStreamType fileType
)
public void LoadFile (
	Stream data, 
	RichTextBoxStreamType fileType
)
public function LoadFile (
	data : Stream, 
	fileType : RichTextBoxStreamType
)

Parameters

data

A stream of data to load into the RichTextBox 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.

You can use this version of the LoadFile method to load the RichTextBox with data from an existing stream of data. The data that is loaded into the control replaces 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 file that has been previously opened into a data stream into the control for manipulation. 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.

This version of the LoadFile method also enables you to specify the type of data that is being loaded into the control. This feature enables you to use data streams that contain data other than rich text format (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 demonstrates using the SaveFile and LoadFile methods with streams. It also demonstrates using the FileDialog.FileName, FileDialog.DefaultExt, SaveFileDialog.CreatePrompt, and SaveFileDialog.OverwritePrompt members.

This is a complete example that is ready to run when you copy it to your project.

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

import System.Windows.Forms.*;

public class Form1 extends System.Windows.Forms.Form
{
    System.Windows.Forms.RichTextBox richTextBox1;
    System.Windows.Forms.Button button1;
    System.Windows.Forms.RichTextBox richTextBox2;
    System.Windows.Forms.Button button2;
    System.Windows.Forms.SaveFileDialog saveFileDialog1;

    public Form1()
    {
        super();
        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.set_Location(new System.Drawing.Point(24, 64));
        this.richTextBox1.set_Name("RichTextBox1");
        this.richTextBox1.set_TabIndex(0);
        this.richTextBox1.set_Text("Type something here.");
        this.button1.set_Location(new System.Drawing.Point(96, 16));
        this.button1.set_Name("Button1");
        this.button1.set_Size(new System.Drawing.Size(96, 24));
        this.button1.set_TabIndex(1);
        this.button1.set_Text("Save To Stream");
        this.button1.add_Click(new System.EventHandler(button1_Click));
        this.richTextBox2.set_Location(new System.Drawing.Point(152, 64));
        this.richTextBox2.set_Name("RichTextBox2");
        this.richTextBox2.set_TabIndex(3);
        this.richTextBox2.set_Text("It will be added to the stream "
            + "and appear here.");
        this.button2.set_Location(new System.Drawing.Point(104, 200));
        this.button2.set_Name("Button2");
        this.button2.set_Size(new System.Drawing.Size(88, 32));
        this.button2.set_TabIndex(4);
        this.button2.set_Text("Save Stream To File");
        this.button2.add_Click(new System.EventHandler(button2_Click));
        this.set_ClientSize(new System.Drawing.Size(292, 266));
        this.get_Controls().Add(this.button2);
        this.get_Controls().Add(this.richTextBox2);
        this.get_Controls().Add(this.button1);
        this.get_Controls().Add(this.richTextBox1);
        this.set_Name("Form1");
        this.set_Text("Form1");
        this.ResumeLayout(false);
    } //Form1

    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

    // Declare a new memory stream.
    private 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(Object sender, System.EventArgs e)
    {
        richTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText);
        userInput.WriteByte(System.Convert.ToByte(13));
        // Display the entire contents of the stream,
        // by setting its position to 0, to RichTextBox2.
        userInput.set_Position(0);
        richTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText);
    } //button1_Click

    // Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    private void button2_Click(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.set_CreatePrompt(true);
        saveFileDialog1.set_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.set_FileName("myText");
        saveFileDialog1.set_DefaultExt("txt");
        saveFileDialog1.set_Filter("Text files (*.txt)|*.txt");
        saveFileDialog1.set_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.Equals(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.set_Position(0);
            userInput.WriteTo(fileStream);
            fileStream.Close();
        }
    } //button2_Click
} //Form1

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

Community Additions

ADD
Show:
© 2015 Microsoft