2014-11-03 34 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

相關問題