Click here to Skip to main content
13,000,569 members (60,940 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 2 Feb 2006

Saving and Restoring RichTextBox Formatted Text. Alternate to SaveFile/LoadFile

, 2 Feb 2006
Rate this:
Please Sign up or sign in to vote.
Overcoming the RichTextBox.LoadFile 'Invalid file format' error on non PlainText.


I have spent some hours searching the web trying to figure out why the RichTextBox LoadFile method fails on anything but PlainText when the System.IO.Stream data parameter is a MemoryStream. On browsing most of the web hits, I didn't find a solution and all of the references to richTextBox1.LoadFile(memoryStream, ...) conveniently used RichTextBoxStreamType.PlainText, which of course works.


The following code is a simple example highlighting the problem. I am simply attempting to transfer the contents of one RichTextBox to another retaining the text format properties.

void Test1() {
MemoryStream ms = new MemoryStream();
// retain formatting - no embedded ole objects
this.richTextBox1.SaveFile(ms, RichTextBoxStreamType.RichNoOleObjs);
// re-position the stream
ms.Position = 0;
// the following line throws an exception 'Invalid file format'
this.richTextBox2.LoadFile(ms, RichTextBoxStreamType.RichNoOleObjs);


After some considerable curses directed both at my computer and my dog, I examined the other RichTextBox properties and success (was embarrassingly simple):

void Test2() {
string rtfText = this.richTextBox1.Rtf;
// process formatted RTF text - i.e. save to database etc.
// ...
// reload formatted RTF text
this.richTextBox2.Rtf = rtfText;

For my purpose, I wanted to expand coded entries to full text descriptions. However, if the formatted text is to be saved to a database, as I will be doing once the edit (and my current coding) is completed, then I will simply say:

dataRow["FormattedText"] = this.richTextBox1.Rtf;

The database column type can be Text or varchar/nvarchar, no BLOB required. Works similarly for XML.


My requirement does not include storing images in the RichTextBox so I have not tested the same. None of the articles I found on my web search made a reference to using the RTF property value as an alternative to SaveFile/LoadFile. It seems that for many situations (e.g. editing/viewing the result in Notepad, database / XML store) saving the RTF property could provide a more flexible solution.


  • 2nd February, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Australia Australia
Applications developer in C, VB, C++, C#

You may also be interested in...


Comments and Discussions

QuestionWhat about appending Pin
art_coding17-Apr-07 22:44
memberart_coding17-Apr-07 22:44 
GeneralThanks... Pin
Bertus Kruger10-Jan-07 11:35
memberBertus Kruger10-Jan-07 11:35 
QuestionRtf propert doesnt get assigned a string Pin
Nayan Choudhary27-Oct-06 9:58
memberNayan Choudhary27-Oct-06 9:58 
AnswerRe: Rtf propert doesnt get assigned a string Pin
darksign30-Apr-07 0:20
memberdarksign30-Apr-07 0:20 
Generalomg you saved me... Pin
roylory13-Jul-06 21:53
memberroylory13-Jul-06 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 2 Feb 2006
Article Copyright 2006 by coleydog
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid