2013-04-21 44 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