RichTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText) RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText) Private void Button1_Click(Object sender, EventArgs e) MemoryStream userInput = new MemoryStream() "It will be added to the stream and appear here."
This.SaveFileDialog1 = new SaveFileDialog() which indicates that the file was saved.ĭialogResult result = SaveFileDialog1->ShowDialog() įileStream = SaveFileDialog1->OpenFile() Call ShowDialog and check for a return value of DialogResult.OK, SaveFileDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" Įnvironment->GetFolderPath(Environment::SpecialFolder::MyDocuments) the filter box and no extension is specified by the user. DefaultExt is only used when "All files" is selected from to text files, and set the initial directory to the Set the file name to myText.txt, set the type filter or overwrite the file if it does exist. prompted to create the file if it doesn't exist Set the properties on SaveFileDialog1 so the user is Void Button2_Click( Object^ /*sender*/, EventArgs^ /*e*/ ) Shows the use of a SaveFileDialog to save a MemoryStream to a file. RichTextBox2->LoadFile( userInput, RichTextBoxStreamType::PlainText ) by setting its position to 0, to RichTextBox2. Display the entire contents of the stream, RichTextBox1->SaveFile( userInput, RichTextBoxStreamType::PlainText ) Void Button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ ) Save the content of RichTextBox1 to the memory stream, This->Controls->Add( this->RichTextBox1 ) This->Controls->Add( this->RichTextBox2 ) Gcnew EventHandler( this, &Form1::Button2_Click ) This->Button2->Text = "Save Stream To File" This->RichTextBox2->Text = "It will be added to the stream " This->RichTextBox2->Name = "RichTextBox2" This->RichTextBox2->Location = Point( 152, 64 ) Gcnew EventHandler( this, &Form1::Button1_Click ) This->Button1->Location = Point( 96, 16 ) This->RichTextBox1->Text = "Type something here." This->RichTextBox1->Name = "RichTextBox1" This->RichTextBox1->Location = Point( 24, 64 ) This->SaveFileDialog1 = gcnew SaveFileDialog This is a complete example that's ready to run when you copy it to your project. It also demonstrates using the FileDialog.FileName, FileDialog.DefaultExt, SaveFileDialog.CreatePrompt, and SaveFileDialog.OverwritePrompt members.
RICH TEXT FILE CODE
The following code example demonstrates using the SaveFile and LoadFile methods with streams. If you want to save the file, you can use the SaveFile method. You can use this method to load a previously created text or RTF document into the control for manipulation. This will cause the values of the Text and Rtf properties to change. When loading a file with the LoadFile method, the contents of the file being loaded replace the entire contents of the RichTextBox control. RichTextBox1.LoadFile(openFile1.FileName) ' Load the contents of the file into the RichTextBox. ' Determine whether the user selected a file from the OpenFileDialog. ' Initialize the OpenFileDialog to look for RTF files. ' Create an OpenFileDialog to request a file to open. OpenFileDialog openFile1 = new OpenFileDialog() RichTextBox1->LoadFile( openFile1->FileName ) Load the contents of the file into the RichTextBox. If ( openFile1->ShowDialog() = System::Windows::Forms::DialogResult::OK & Determine whether the user selected a file from the OpenFileDialog. Initialize the OpenFileDialog to look for RTF files. OpenFileDialog^ openFile1 = gcnew OpenFileDialog Create an OpenFileDialog to request a file to open. This example requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1. If the file is not, the example code will throw an exception. The code then loads that file assuming that it is an RTF document file. The example uses the OpenFileDialog class to display a dialog to request the file from the user. The following code example opens an RTF file into the RichTextBox control.
The file being loaded is not an RTF document.