2015-04-01 45 views
0

我有我讀的文本文件到使用按鈕一個StreamReader點擊 我已格式的文本文件,我希望它內顯示的方式一個RichTextBox該文本文件,使用段落,但是當我添加文本到RichTextBox格式消失。 有沒有辦法從文本文件讀入文本時,我可以格式化文本單獨段落?格式文本

這是我的代碼:

  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]參考我已經添加到我的文本文件,從而讀取器僅讀取其間這些標籤和僅顯示選擇的文本標籤其間

感謝您的幫助

+0

是它的Windows Phone或者Windows窗體或WPF? – 2015-04-01 10:25:04

+0

@AnupSharma它是一個視窗形式應用 – FlipperFlapper 2015-04-01 10:27:12

回答

0

通過StreamReader.ReadLine()返回不包含終止回車或換行的字符串。因此,你需要在每行的末尾手動將其添加到您的RichTextBox

   txtContent.AppendText(line.Replace("[7]", "").Replace("[/7]", "")); 
       txtContent.AppendText(Environment.NewLine);