1
votes

I'm trying to use a RichTextBox as a status display but it is not updating every time I append to the text of the RichTextBox. A regular multiline TextBox will update everytime new text is appended to it, but I would prefer to use the RichTextBox. Is there some sort of property that needs to be set to see the updates?

UPDATE: Also, how would I automatically make it scroll down every a message is displayed?

Like I said, if I replace the RichTextBox with a TextBox, I can see each line of the messages as they are getting written out and it will also automatically scroll down to the bottom to display the latest text added.

void UpdateStatus(string textMessage)
{
  if (InvokeRequired)
  {
    BeginInvoke(new MethodInvoker(() => UpdateStatus(textMessage)));
    return;
  }

  richTextBox.AppendText(textMessage + Environment.NewLine);
}
3
Post your code what have you tried and is not working ??Alaa Jabre
Please don't prefix your titles with "C#" and such. That's what the tags are for.John Saunders

3 Answers

3
votes

An easy and quick fix would be

Application.DoEvents()

1
votes

To make RichTextBox scroll automatically, please use ScrollToCaret method:

richTextBox.AppendText("Hello");    
richTextBox.ScrollToCaret();
0
votes

Your code looks like it should be working, make sure you check the right InvokeRequired flag:

void UpdateStatus(string textMessage)
{
  if (richTextBox.InvokeRequired)
  {
    richTextBox.Invoke(new MethodInvoker(() => UpdateStatus(textMessage)));
    return;
  }

  richTextBox.AppendText(textMessage + Environment.NewLine);
}