简体   繁体   English

RichTextBox新文本追加在顶部

[英]RichTextBox new Text append on top

I have a RichtTextBox in my C# application that shows a log to the user. 我的C#应用程序中有一个RichtTextBox ,它向用户显示一个日志。 The problem is that newly inserted text appends below the old text, but I want to append it on top of the old text. 问题是新插入的文本appends在旧文本下面,但我想appendappend在旧文本的顶部。

For example, When I append the text "Newtext" it looks like this: 例如,当我附加文本“Newtext”时,它看起来像这样:

RichtTextBox : RichtTextBox

|---------------------
|Oldtext             |
|Newtext             |
|---------------------

But it needs to look like this: 但它需要看起来像这样:

RichTextBox : RichTextBox

|---------------------
|Newtext             |
|Oldtext             |
|---------------------

This is the code I'm using for filling my RichTextBox : 这是我用来填充RichTextBox的代码:

public void DisplayLog(string logtext)
        {
            if (logtext != "")
            {
                if (this.txtLog.InvokeRequired && !txtLog.IsDisposed)
                {
                    Invoke(new MethodInvoker(delegate()
                        {
                            txtLog.AppendText(DateTime.UtcNow + ": " + logtext + "\n");
                        }));
                }
                else if (!txtLog.IsDisposed)
                {
                    txtLog.AppendText(DateTime.UtcNow + ": " + logtext + "\n");
                }
            }
        }

Can somebody help me out please? 有人可以帮帮我吗?

Answer: 回答:

Inserting at top of richtextbox 插入richtextbox的顶部

使用插入

txtLog.Text =  txtLog.Text.Insert(0,DateTime.UtcNow + ": " + logtext + "\n");

I think txtlog is the RichTextBox and you should prepend this. 我认为txtlog是RichTextBox,你应该在前面加上它。

To do this go at start using 要做到这一点,请开始使用

txtlog .SelectionStart = 0;
txtlog .SelectionLength = 0;
txtlog .SelectedText = (DateTime.UtcNow + ": " + logtext + "\n");

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

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