如何在Richtextbox C#中逐个删除行

时间:2016-09-14 17:56:58

标签: c# richtextbox

我使用此代码在richtextbox中逐个删除行,但仍然留空行(空格)。

{{1}}

如何逐行删除行而没有空行(空格)?

1 个答案:

答案 0 :(得分:0)

// Gets the number of newline characters in your rich text box
var numberOfNewLines = richTextBox1.Text.Count(r => r == '\n');

for (var i = 0; i < numberOfNewLines; i++)
{
     // Finds the first occurance of the newline character
     var newlineCharacterIndex = richTextBox1.Text.IndexOf('\n') + 1;

     // Replaces the rich textbox text with everything but the above line
     richTextBox1.Text = richTextBox1.Text.Substring(newlineCharacterIndex);

     MessageBox.Show("OK!");
}

// Removes the final line.
richTextBox1.Text = string.Empty;

我认为你是在正确的轨道上,但你这样做的方式只是删除线的内容而不是线本身。

相关问题