Saves the contents of a RichTextBox control to an open data stream.
Namespace:
System.Windows.Forms
Assembly:
System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Sub SaveFile ( _
data As Stream, _
fileType As RichTextBoxStreamType _
)
Dim instance As RichTextBox
Dim data As Stream
Dim fileType As RichTextBoxStreamType
instance.SaveFile(data, fileType)
public void SaveFile(
Stream data,
RichTextBoxStreamType fileType
)
public:
void SaveFile(
Stream^ data,
RichTextBoxStreamType fileType
)
public function SaveFile(
data : Stream,
fileType : RichTextBoxStreamType
)
| Exception | Condition |
|---|
| ArgumentException | An invalid file type is specified in the fileType parameter. |
| IOException | An error occurs in saving the contents of the control to a file. |
This version of the SaveFile method enables you to save the entire contents of the control to the data stream that is already opened. The data stream can then save the information to a file. You can use the LoadFile method to load the contents of a file into the RichTextBox.
This version of the SaveFile method also enables you to specify a data format of the information that will be sent to the Stream object.
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.
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.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
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();
}
}
}
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
public private:
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()
: Form()
{
userInput = gcnew System::IO::MemoryStream;
this->RichTextBox1 = gcnew System::Windows::Forms::RichTextBox;
this->Button1 = gcnew System::Windows::Forms::Button;
this->RichTextBox2 = gcnew System::Windows::Forms::RichTextBox;
this->Button2 = gcnew System::Windows::Forms::Button;
this->SaveFileDialog1 = gcnew System::Windows::Forms::SaveFileDialog;
this->SuspendLayout();
this->RichTextBox1->Location = System::Drawing::Point( 24, 64 );
this->RichTextBox1->Name = "RichTextBox1";
this->RichTextBox1->TabIndex = 0;
this->RichTextBox1->Text = "Type something here.";
this->Button1->Location = System::Drawing::Point( 96, 16 );
this->Button1->Name = "Button1";
this->Button1->Size = System::Drawing::Size( 96, 24 );
this->Button1->TabIndex = 1;
this->Button1->Text = "Save To Stream";
this->Button1->Click += gcnew System::EventHandler( this, &Form1::Button1_Click );
this->RichTextBox2->Location = 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 = System::Drawing::Point( 104, 200 );
this->Button2->Name = "Button2";
this->Button2->Size = System::Drawing::Size( 88, 32 );
this->Button2->TabIndex = 4;
this->Button2->Text = "Save Stream To File";
this->Button2->Click += gcnew System::EventHandler( this, &Form1::Button2_Click );
this->ClientSize = 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 );
}
// Declare a new memory stream.
System::IO::MemoryStream^ userInput;
private:
// Save the content of RichTextBox1 to the memory stream,
// appending a LineFeed character.
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.
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.
System::Windows::Forms::DialogResult result = SaveFileDialog1->ShowDialog();
System::IO::Stream^ fileStream;
if ( result == ::DialogResult::OK )
{
fileStream = SaveFileDialog1->OpenFile();
userInput->Position = 0;
userInput->WriteTo( fileStream );
fileStream->Close();
}
}
};
int main()
{
Application::Run( gcnew Form1 );
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference