在 Visual C# 中从 RichTextBox 中删除 RichText 格式 [英] Removing RichText Formatting from RichTextBox in Visual C#

查看:83
本文介绍了在 Visual C# 中从 RichTextBox 中删除 RichText 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 c# 开发高级富文本编辑器,但偶然发现了一个我似乎无法理解的问题.

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

MyRichTextBox.SaveFile(filepath, PlainText);

但问题是,当他们在记事本中查看该文件(应该保存为纯文本)时,它会显示所有 RTF 格式代码,这使得他们的文档无法读取.有没有人知道从 RichTextBox 中删除格式代码而不必做太多事情的任何其他方法?

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?

我的意思是,如果要为这样一个简单的任务找到解决方法需要做很多工作,我不妨从头开始创建自己的 RichTextBox 控件.我敢肯定这很难做到,但至少我知道它会起作用......

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...

谢谢大家,杰森.

推荐答案

应该这样做:

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

或者,获取 RTF 的方法是:

Alternatively, the way to get the RTF is:

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

这篇关于在 Visual C# 中从 RichTextBox 中删除 RichText 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆