Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a new line to a richtextbox without making the last line blank?

I'm making a log system for a program im creating and I currently have it to where it does this:

void outToLog(string output)
{
    logRichTextBox.AppendText(output + "\r\n");
    logRichTextBox.ScrollToCaret();
}

But it ends up outputting the last line of the RichTextBox as blank (because I'm using \n) and I want the last line to just be whatever the output was, not a blank line. An alternative was for me to put the "\r\n" at the beginning, but this just has the same affect except its at the beginning of the RichTextBox.

help? thanks

like image 323
Connor Roberts Avatar asked Apr 14 '16 09:04

Connor Roberts


Video Answer


2 Answers

Append the text after the newline.

void outToLog(string output)
{
    logRichTextBox.AppendText("\r\n" + output);
    logRichTextBox.ScrollToCaret();
}

If you don't want the newline at the start, check the TextBox for empty text and only add the \r\n when the TextBox is not empty.

void outToLog(string output)
{
    if(!string.IsNullOrWhiteSpace(logRichTextBox.Text))
    {
        logRichTextBox.AppendText("\r\n" + output);
    }
    else
    {
        logRichTextBox.AppendText(output);
    }
    logRichTextBox.ScrollToCaret();
}
like image 74
Adwaenyth Avatar answered Sep 19 '22 06:09

Adwaenyth


Eh, why not check? If text box is empty - just put the output, but if text box is not empty, add a new line and then append the the output.

void outToLog(string output)
{
    if (String.IsNullOrEmpty(logRichTextBox.Text)) 
        logRichTextBox.AppendText(output);
    else
        logRichTextBox.AppendText(Environment.NewLine + output);

    logRichTextBox.ScrollToCaret();
}
like image 41
Dmitry Bychenko Avatar answered Sep 21 '22 06:09

Dmitry Bychenko