views:

86

answers:

2

How can I load a pre-existing .txt file as a .rtf file in my C# code if I want to display it on a richTextBox? I am running Visual Studios Windows Application.

Thank you very much.

+5  A: 

Try

string text = File.ReadAllText("filename.txt");
MyRichTextBox.Rtf = text; // oops, flying blind here
Will
As opposed to `MyRichTextBox.Rtf = text;`
Joel Coehoorn
@joel you know you could have fixed my bug and saved me some embarassment but nOOOOOoooo....
Will
+4  A: 

You might also try

MyRichTextBox.LoadFile("filename.txt", RichTextBoxStreamType.PlainText); 

edit: corrected to add RichTextBoxStreamType parameter.

Toby
I don't think that will work since richTextBox only supports .rtf, according to the yellow box description inVisual Studios.
George Tyler
I stand corrected. It should be:MyRichTextBox.LoadFile("filename.txt", RichTextBoxStreamType.PlainText);
Toby
@Toby: Instead of giving a correction in a comment, you should edit your own answer and put it there.
Oliver
Thanks, Oliver.As you can tell, I'm new around here (at least in a posting capacity.) I'm still trying to figure out how Joel embedded code markup in a comment. :-/
Toby