Hi friends!
I'm doing a small project in VC# 2008.
In the main form (called myForm) I have a richTextBox (called myRichTextBox) and a button (called myButton). The thing is: when I click on the button, the last line from the richTextBox must be deleted. I'm doing like this:

private void myButton_Click(object sender, EventArgs e)
        {
            if (myRichTextBox.TextLength != 0)
            {
                int totalCharacters = myRichTextBox.Text.Trim().Length;
                int totalLines = myRichTextBox.Lines.Length;
                string lastLine = myRichTextBox.Lines[totalLines - 1] + "\n";
                string copyOfLastLine = myRichTextBox.Lines[totalLines - 1];
                if (totalLines > 1)
                {
                    string newstring = myRichTextBox.Text.Substring(0, totalCharacters - lastLine.Length);
                    myRichTextBox.Text = newstring;
                 }
                else
                {
                    myRichTextBox.Text = "";
                }

            } 

        }

Ok! So it's working well when there's not a blank line in the richTextBox. Otherwise it wouldn't.
I really need hepl. I was working on it 3 days. No good result.
Thanks in advanced!

Dani AI

Generated

You are running into two separate issues: (1) using Text.Trim() changes the length you subtract from, so your substring math no longer matches what Lines reports; and (2) hard-coding "\n" on Windows can miss the actual CRLF pair. As showed, replacing the Lines array is the simplest fix. If you want to avoid rebuilding the entire text (and keep any rich formatting intact), select and delete the last line by index instead of doing string arithmetic.

Here is a selection-based approach that deletes exactly one last line whether it is empty or not, and handles CRLF vs LF correctly:

private void myButton_Click(object sender, EventArgs e)
{
    var rtb = myRichTextBox;
    if (rtb.TextLength == 0) return;

    int lastLine = rtb.Lines.Length - 1;
    int start = rtb.GetFirstCharIndexFromLine(lastLine);
    int end = rtb.TextLength;

    // If the last line is blank (text ends with a newline), delete only that newline
    if (start == end && end > 0)
    {
        if (end >= 2 && rtb.Text[end - 2] == '\r' && rtb.Text[end - 1] == '\n')
            start = end - 2;         // CRLF
        else
            start = end - 1;         // lone LF
    }

    rtb.Select(start, end - start);
    rtb.SelectedText = string.Empty;
}

Notes and tips:

  • Do not call Trim() before computing indexes; it changes what you think the last line is.
  • If your intent is to remove the last non-empty line (ignoring trailing blank lines), first walk backward over rtb.Lines while the entries are empty, then compute start for that line and delete as above.
  • For large texts, you can wrap the selection and delete with SuspendLayout()/ResumeLayout() to avoid flicker.
private void button1_Click_1(object sender, EventArgs e) {
    List<string> myList = richTextBox1.Lines.ToList();
    if (myList.Count > 0) {
        myList.RemoveAt(myList.Count - 1);
        richTextBox1.Lines = myList.ToArray();
        richTextBox1.Refresh();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.