Max*_*Max 3 c# logging append visual-studio
我有一个RichtTextBox在我的C#应用程序,显示日志给用户。问题是新插入的文字appends在旧文字下方,但我想append在旧文字上方。
例如,当我添加文本“ Newtext”时,它看起来像这样:
RichtTextBox:
|---------------------
|Oldtext |
|Newtext |
|---------------------
Run Code Online (Sandbox Code Playgroud)
但它需要看起来像这样:
RichTextBox:
|---------------------
|Newtext |
|Oldtext |
|---------------------
Run Code Online (Sandbox Code Playgroud)
这是我用来填充我的代码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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
回答:
使用插入
txtLog.Text = txtLog.Text.Insert(0,DateTime.UtcNow + ": " + logtext + "\n");
Run Code Online (Sandbox Code Playgroud)