我希望在刪除所有空行時保留文本中的斜體。我似乎無法做到。如何在保留ctext中richtextbox格式的同時刪除空行
我使用了這段代碼。它刪除空行,但文本的斜體。
richTextBox1.Text = Regex.Replace(richTextBox1.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
我希望在刪除所有空行時保留文本中的斜體。我似乎無法做到。如何在保留ctext中richtextbox格式的同時刪除空行
我使用了這段代碼。它刪除空行,但文本的斜體。
richTextBox1.Text = Regex.Replace(richTextBox1.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
黃金法則約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);
}
對於多針,您需要的代碼多次打電話,我怕..
你可以嘗試從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);
它將保留文本的格式並刪除所有空行。
對於包含'\\ par' -1的假設'Environment.NewLine'是個好主意。它幾乎永遠是.. – TaW 2014-11-03 11:56:47
非常感謝。此代碼完美工作。 – Zain 2014-11-03 14:30:51