2014-11-03 20 views

回答

0

黄金法则约RichTextBoxes从未直接改变Text,一旦任何格式。

要添加您使用AppendText并更改您需要使用Select, Cut, Copy & Paste..方法来保留格式!

string needle = "\r\r"; // only one possible cause of empty lines 

int p1 = richTextBox1.Find(needle); 
while (p1 >= 0) 
{ 
    richTextBox1.SelectionStart = p1; 
    richTextBox1.Select(p1, needle.Length); 
    richTextBox1.Cut(); 
    p1 = richTextBox1.Find(needle); 
} 

对于多针,您需要的代码多次打电话,我怕..

+0

非常感谢。此代码完美工作。 – Zain 2014-11-03 14:30:51

1

你可以尝试从RTF文本删除空行。下面给出的示例代码将适用于您。

String[] allLines = richTextBox1 
        .Rtf 
        .Split(new string[] { Environment.NewLine },StringSplitOptions.None); 

dynamic linesWithoutEmptyLines = from itm in allLines 
           where itm.Trim() != "\\par" 
           select itm; 

richTextBox1.Rtf = string 
        .Join(Environment.NewLine, linesWithoutEmptyLines); 

它将保留文本的格式并删除所有空行。

+0

对于包含'\\ par' -1的假设'Environment.NewLine'是个好主意。它几乎永远是.. – TaW 2014-11-03 11:56:47

相关问题