I have a RichtTextBox
in my C#
application that shows a log to the user. The problem is that newly inserted text appends
below the old text, but I want to append
it on top of the old text.
For example, When I append the text "Newtext" it looks like this:
RichtTextBox
:
|---------------------
|Oldtext |
|Newtext |
|---------------------
But it needs to look like this:
RichTextBox
:
|---------------------
|Newtext |
|Oldtext |
|---------------------
This is the code I'm using for filling my 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
Use Insert
txtLog.Text = txtLog.Text.Insert(0,DateTime.UtcNow + ": " + logtext + "\n");
I think txtlog is the RichTextBox and you should prepend this.
To do this go at start using
txtlog .SelectionStart = 0;
txtlog .SelectionLength = 0;
txtlog .SelectedText = (DateTime.UtcNow + ": " + logtext + "\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With