删除 .NET RichTextBox 中的特定行

发布于 2024-08-02 09:29:43 字数 32 浏览 5 评论 0原文

如何删除 RichTextBox 中的特定文本行?

How can I delete a specific line of text in a RichTextBox ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

回眸一笑 2024-08-09 09:29:43

另一个解决方案:

private void DeleteLine(int a_line)
{
    int start_index = richTextBox.GetFirstCharIndexFromLine(a_line);
    int count = richTextBox.Lines[a_line].Length;

    // Eat new line chars
    if (a_line < richTextBox.Lines.Length - 1)
    {
        count += richTextBox.GetFirstCharIndexFromLine(a_line + 1) -
            ((start_index + count - 1) + 1);
    }

    richTextBox.Text = richTextBox.Text.Remove(start_index, count);
}

Another solution:

private void DeleteLine(int a_line)
{
    int start_index = richTextBox.GetFirstCharIndexFromLine(a_line);
    int count = richTextBox.Lines[a_line].Length;

    // Eat new line chars
    if (a_line < richTextBox.Lines.Length - 1)
    {
        count += richTextBox.GetFirstCharIndexFromLine(a_line + 1) -
            ((start_index + count - 1) + 1);
    }

    richTextBox.Text = richTextBox.Text.Remove(start_index, count);
}
乱了心跳 2024-08-09 09:29:43

这也可以解决问题(如果您可以在表单代码中处理诸如 ++ 之类的事情)。保留文本格式。请记住“只读”属性对您和用户都有效。

richTextBox.SelectionStart = richTextBox.GetFirstCharIndexFromLine(your_line);
richTextBox.SelectionLength = this.richTextBox.Lines[your_line].Length+1;
this.richTextBox.SelectedText = String.Empty;

This also could do the trick (if you can handle things such as ++ in forms code). Keeps the text format. Just remember "ReadOnly" attribute work for both you and user.

richTextBox.SelectionStart = richTextBox.GetFirstCharIndexFromLine(your_line);
richTextBox.SelectionLength = this.richTextBox.Lines[your_line].Length+1;
this.richTextBox.SelectedText = String.Empty;
风吹雨成花 2024-08-09 09:29:43

试试这个:

Dim lst As New ListBox  
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
            Me.Controls.Add(lst)  
            For Each cosa As String In Me.RichTextBox1.Lines  
                lst.Items.Add(cosa)  
            Next  
            lst.Items.RemoveAt(2) 'the integer value must be the line that you want to remove -1  
            Me.RichTextBox1.Text = String.Empty  
            For i As Integer = 0 To lst.Items.Count - 1  
                If Me.RichTextBox1.Text = String.Empty Then  
                    Me.RichTextBox1.Text = lst.Items.Item(i)  
                Else  
                    MeMe.RichTextBox1.Text = Me.RichTextBox1.Text & Environment.NewLine & lst.Items.Item(i).ToString  
                End If  
            Next  
        End Sub

http:// /social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/63647481-743d-4e55-9043-e0db5106a03a/

Try this:

Dim lst As New ListBox  
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
            Me.Controls.Add(lst)  
            For Each cosa As String In Me.RichTextBox1.Lines  
                lst.Items.Add(cosa)  
            Next  
            lst.Items.RemoveAt(2) 'the integer value must be the line that you want to remove -1  
            Me.RichTextBox1.Text = String.Empty  
            For i As Integer = 0 To lst.Items.Count - 1  
                If Me.RichTextBox1.Text = String.Empty Then  
                    Me.RichTextBox1.Text = lst.Items.Item(i)  
                Else  
                    MeMe.RichTextBox1.Text = Me.RichTextBox1.Text & Environment.NewLine & lst.Items.Item(i).ToString  
                End If  
            Next  
        End Sub

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/63647481-743d-4e55-9043-e0db5106a03a/

讽刺将军 2024-08-09 09:29:43

不知道有没有一种简单的方法可以一步完成。您可以在富文本框的 .Text 属性上使用 .Split 函数来获取行数组

string[] lines = richTextBox1.Text.Split( "\n".ToCharArray() )

,然后在删除所需的行并将其复制回.Text 富文本框的属性。

这是一个简单的示例:

        string[] lines = richTextBox1.Text.Split("\n".ToCharArray() );


        int lineToDelete = 2;           //O-based line number

        string richText = string.Empty;

        for ( int x = 0 ; x < lines.GetLength( 0 ) ; x++ )
        {
            if ( x != lineToDelete )
            {
                richText += lines[ x ];
                richText += Environment.NewLine;
            }
        }

        richTextBox1.Text = richText;

如果您的富文本框将包含超过 10 行左右,那么最好使用 StringBuilder 而不是字符串来组成新文本。

I don't know if there is an easy way to do it in one step. You can use the .Split function on the .Text property of the rich text box to get an array of lines

string[] lines = richTextBox1.Text.Split( "\n".ToCharArray() )

and then write something to re-assemble the array into a single text string after removing the line you wanted and copy it back to the .Text property of the rich text box.

Here's a simple example:

        string[] lines = richTextBox1.Text.Split("\n".ToCharArray() );


        int lineToDelete = 2;           //O-based line number

        string richText = string.Empty;

        for ( int x = 0 ; x < lines.GetLength( 0 ) ; x++ )
        {
            if ( x != lineToDelete )
            {
                richText += lines[ x ];
                richText += Environment.NewLine;
            }
        }

        richTextBox1.Text = richText;

If your rich text box was going to have more than 10 lines or so it would be a good idea to use a StringBuilder instead of a string to compose the new text with.

凉栀 2024-08-09 09:29:43

此处找到的文本范围中找到要删除的文本文本清空,现在它从文档中消失了。

Find the text to delete in a text range, found here Set the text to empty, and now it is gone form the document.

誰認得朕 2024-08-09 09:29:43

基于tomanu的解决方案,但没有开销,

int start_index = LogBox.GetFirstCharIndexFromLine(linescount);
int count = LogBox.GetFirstCharIndexFromLine(linescount + 1) - start_index;
LogBox.Text = LogBox.Text.Remove(start_index, count);

请注意,我的行数是行数 - 2。

Based on tomanu's solution but without overhead

int start_index = LogBox.GetFirstCharIndexFromLine(linescount);
int count = LogBox.GetFirstCharIndexFromLine(linescount + 1) - start_index;
LogBox.Text = LogBox.Text.Remove(start_index, count);

note that my linescount here is linescount - 2.

追我者格杀勿论 2024-08-09 09:29:43
int LineToDelete = 50;
List<string> lines = richTextBox1.Lines.ToList();
lines.Remove(LineToDelete);
richTextBox1.Lines = lines.ToArray();
int LineToDelete = 50;
List<string> lines = richTextBox1.Lines.ToList();
lines.Remove(LineToDelete);
richTextBox1.Lines = lines.ToArray();
云柯 2024-08-09 09:29:43

这是我的单元测试实现。

public static void DeleteLine([NotNull] this RichTextBox pRichTextBox, int pLineIndex) {
   if (pLineIndex < 0 || pLineIndex >= pRichTextBox.Lines.Length)
      throw new InvalidOperationException("There is no such line.");

   var start = pRichTextBox.GetFirstCharIndexFromLine(pLineIndex);
   var isLastLine = pLineIndex == pRichTextBox.Lines.Length - 1;
   var nextLineIndex = pLineIndex + 1;

   var end = isLastLine
      ? pRichTextBox.Text.Length - 1
      : pRichTextBox.GetFirstCharIndexFromLine(nextLineIndex) - 1;

   var length = end - start + 1;
   pRichTextBox.Text = pRichTextBox.Text.Remove(start, length);
}

单元测试:(

使用 \n 而不是 Environment.NewLine 因为至少对我来说 RTB 会自动将 \r\n 替换为 >\n)

[TestMethod]
public void TestDeleteLine_SingleLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\n";
   rtb.DeleteLine(0);
   var expected = "";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_BlankLastLine() {
   var rtb = new RichTextBox();
   rtb.Text = "\n";
   rtb.DeleteLine(1);
   var expected = "\n";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_SingleLineNoEOL() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.";
   rtb.DeleteLine(0);
   var expected = "";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_FirstLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\nThis is line2.\nThis is line3.";
   rtb.DeleteLine(0);
   var expected = "This is line2.\nThis is line3.";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_MiddleLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\nThis is line2.\nThis is line3.";
   rtb.DeleteLine(1);
   var expected = "This is line1.\nThis is line3.";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_LastLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\nThis is line2.\nThis is line3.";
   rtb.DeleteLine(2);
   var expected = "This is line1.\nThis is line2.\n";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_OneBlankLine() {
   var rtb = new RichTextBox();
   rtb.Text = "\n";
   rtb.DeleteLine(0);
   var expected = "";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_BlankLines() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n\n\n\n";
   rtb.DeleteLine(2);
   var expected = "\n\n\n\n";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod, ExpectedException(typeof(InvalidOperationException))]
public void TestDeleteLine_Exception_BeforeFront() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n\n\n\n";
   rtb.DeleteLine(-1);
}

[TestMethod, ExpectedException(typeof(InvalidOperationException))]
public void TestDeleteLine_Exception_AfterEnd() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n";
   rtb.DeleteLine(3);
}

Here is my unit tested implementation.

public static void DeleteLine([NotNull] this RichTextBox pRichTextBox, int pLineIndex) {
   if (pLineIndex < 0 || pLineIndex >= pRichTextBox.Lines.Length)
      throw new InvalidOperationException("There is no such line.");

   var start = pRichTextBox.GetFirstCharIndexFromLine(pLineIndex);
   var isLastLine = pLineIndex == pRichTextBox.Lines.Length - 1;
   var nextLineIndex = pLineIndex + 1;

   var end = isLastLine
      ? pRichTextBox.Text.Length - 1
      : pRichTextBox.GetFirstCharIndexFromLine(nextLineIndex) - 1;

   var length = end - start + 1;
   pRichTextBox.Text = pRichTextBox.Text.Remove(start, length);
}

Unit tests:

(used \n instead of Environment.NewLine since at least for me RTB is automatically replacing \r\n with just \n)

[TestMethod]
public void TestDeleteLine_SingleLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\n";
   rtb.DeleteLine(0);
   var expected = "";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_BlankLastLine() {
   var rtb = new RichTextBox();
   rtb.Text = "\n";
   rtb.DeleteLine(1);
   var expected = "\n";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_SingleLineNoEOL() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.";
   rtb.DeleteLine(0);
   var expected = "";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_FirstLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\nThis is line2.\nThis is line3.";
   rtb.DeleteLine(0);
   var expected = "This is line2.\nThis is line3.";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_MiddleLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\nThis is line2.\nThis is line3.";
   rtb.DeleteLine(1);
   var expected = "This is line1.\nThis is line3.";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_LastLine() {
   var rtb = new RichTextBox();
   rtb.Text = "This is line1.\nThis is line2.\nThis is line3.";
   rtb.DeleteLine(2);
   var expected = "This is line1.\nThis is line2.\n";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_OneBlankLine() {
   var rtb = new RichTextBox();
   rtb.Text = "\n";
   rtb.DeleteLine(0);
   var expected = "";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod]
public void TestDeleteLine_BlankLines() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n\n\n\n";
   rtb.DeleteLine(2);
   var expected = "\n\n\n\n";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod, ExpectedException(typeof(InvalidOperationException))]
public void TestDeleteLine_Exception_BeforeFront() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n\n\n\n";
   rtb.DeleteLine(-1);
}

[TestMethod, ExpectedException(typeof(InvalidOperationException))]
public void TestDeleteLine_Exception_AfterEnd() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n";
   rtb.DeleteLine(3);
}
卖梦商人 2024-08-09 09:29:43

有很多好的答案,但我发现很多都太复杂了。

string[] LinesArray = this.richTextBox1.Lines;

this.richTextBox1.Clear();

for (int line = 0; line < LinesArray.Length; line++)
{
if (!LinesArray[line].Contains("< Test Text To Remove >"))
{
this.richTextBox1.AppendText(LinesArray[line] + Environment.NewLine);
}
}

我希望这对其他人有帮助;0)

Lots of good answers, but I find many far to complicated.

string[] LinesArray = this.richTextBox1.Lines;

this.richTextBox1.Clear();

for (int line = 0; line < LinesArray.Length; line++)
{
if (!LinesArray[line].Contains("< Test Text To Remove >"))
{
this.richTextBox1.AppendText(LinesArray[line] + Environment.NewLine);
}
}

I hope this helps others some ;0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文