Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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.

Failure

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

Success

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.

Conclusion

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.

History

  • 2nd February, 2006: Initial post

License

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

Share

About the Author

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

Comments and Discussions

 
QuestionWhat about appending Pinmemberart_coding17-Apr-07 22:44 
GeneralThanks... PinmemberBertus Kruger10-Jan-07 11:35 
QuestionRtf propert doesnt get assigned a string PinmemberNayan Choudhary27-Oct-06 9:58 
AnswerRe: Rtf propert doesnt get assigned a string Pinmemberdarksign30-Apr-07 0:20 
Generalomg you saved me... Pinmemberroylory13-Jul-06 21:53 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 2 Feb 2006
Article Copyright 2006 by coleydog
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid