2013-04-21 28 views
1

Helo to all。我有一個名爲mytext.txt一個文本文件,裏面的文字有這樣的格式:在richedTextBox中自定義打印文本文件

<name1>Myname 
age 
address 
postal code 
<name2>Myname 
age 
address 
postal code 
....(more of the same) 

我的問題是,我必須打印在RichTextBox這個文本格式是這樣的:

<name1>Myname 
    -age 
    -address 
    -postal code 
<name2>Myname 
    -age 
    -address 
    -postal code 
....(more of the same) 

任何想法,我必須如何做到這一點?

+0

你嘗試過什麼?另外,你使用的是什麼UI框架?的WinForms? WPF?還有別的嗎? – svick 2013-04-21 12:15:00

回答

0
var writer = new StringBuilder(); 

foreach (var line in File.ReadAllLines("mytext.txt")) 
{ 
    writer.AppendLine(!line.StartsWith("<") ? string.Format(" -{0}", line) : line); 
} 

richTextBox1.Text = writer.ToString(); 
+0

這一個很好。 我做的litle變化是這樣的:stirng.Format(「\ t- {0}」:line); – user2304126 2013-04-21 12:33:26

3

我不會給你確切的代碼,但我可以給你一個什麼樣的工作算法將看起來像一個僞代碼表示:

function printTextFile() 
{ 
    for(every line in the text file) 
     // So start the loop through each line. 
     if(current line does not start with "<") 
     { 
      prefix:= " -". 
      // So we're not at a title part. 
     } 

     print(prefix + current line). 
     // Print out the line with the indent. 
     prefix:= "". 
     // reset the prefix. 
    end for loop. 
} 
+1

+1用於回答學習曲線 – Sayse 2013-04-21 11:07:40