我有一个丰富的文本框,我将一个文本文件读入使用按钮点击一个流阅读器,我已经格式化了文本文件的方式,我希望它显示在文本文件中使用段落,但当我将文本添加到丰富文本框时,格式将消失。当从文本文件读入文本框时,是否可以将文本格式化为单独的段落?
这是我的代码:
using (StreamReader reader = new StreamReader(@"F:\test.txt"))
{
bool content = false;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("[7]"))
{
content = true;
continue;
}
if (content == true)
{
txtContent.AppendText(line.Replace("[7]", "").Replace("[/7]", ""));
}
if (line.Contains("[/7]"))
{
content = false;
break;
}
}
}7和/7引用了我添加到文本文件中的标记,这样读取器只能在这些标记之间读取,并且只在中间显示选定的文本。
谢谢你的帮忙
发布于 2015-04-01 15:35:21
StreamReader.ReadLine()返回的字符串不包含终止回车或行提要。因此,您需要在每一行的末尾手动将其附加到RichTextBox中:
txtContent.AppendText(line.Replace("[7]", "").Replace("[/7]", ""));
txtContent.AppendText(Environment.NewLine);https://stackoverflow.com/questions/29387943
复制相似问题