简体   繁体   English

如何在不将最后一行留空的情况下向richtextbox添加新行?

[英]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. 但它最终将RichTextBox的最后一行输出为空白(因为我正在使用\\n )并且我希望最后一行只是输出,而不是空行。 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 . 另一种选择是让我把"\\r\\n"放在开头,但除了它在RichTextBox的开头之外,它具有相同的效果。

help? 救命? thanks 谢谢

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. 如果您不想在开始时使用换行符,请检查TextBox是否为空文本,并仅在TextBox不为空时添加\\r\\n

void outToLog(string output)
{
    if(!string.IsNullOrWhiteSpace(logRichTextBox.Text))
    {
        logRichTextBox.AppendText("\r\n" + output);
    }
    else
    {
        logRichTextBox.AppendText(output);
    }
    logRichTextBox.ScrollToCaret();
}

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM