Removing RichText Formatting from RichTextBox in Visual C#

别来无恙 提交于 2019-12-24 06:46:43

问题


I'm developing an advanced rich text editor in c# but have stumbled upon a problem that I can't seem to grasp.

I have been trying to let users save their documents as Text files (plain text). By using the following:

MyRichTextBox.SaveFile(filepath, PlainText);

But the problem is that when they view that file (which should have been saved as plain text) in Notepad, it shows up with all of the RTF formatting codes whish makes their documents unreadable. Does anybody know of any other way to remove formatting codes from a RichTextBox without having to do too much?

I mean, if there's going to be alot of work involved in finding a workaround to such a simple task, I might as well just create my own RichTextBox control from scratch. Which I'm sure would be very hard to do, but atleast I know that it'll work...

... Sorry for ranting on a little there, hehe...

Thanks All, Jason.


回答1:


This should do it:

String txt = MyRichTextBox.Text
File.WriteAllText(filepath, txt);

Alternatively, the way to get the RTF is:

String rft = MyRichTextBox.Rtf;
File.WriteAllText(filepath, rtf);



回答2:


Have you tried:

RichTextBoxStreamType.PlainText

instead of:

PlainText


来源:https://stackoverflow.com/questions/1245758/removing-richtext-formatting-from-richtextbox-in-visual-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!