[SOLVED] Update richTextBox in Visual C#?

Landmarkwings

New member
Joined
Jan 29, 2016
Posts
2
I currently have a problem when I try to update my Text Box. I'm able to update it once but not again.
For example in my Start method:
Code:
//This will work
public void Start()
{
richTextBox1.Text = "Chat Logs:" + Environment.NewLine;
richTextBox1.AppendText("Test" + Environment.NewLine);
}
But I'm trying to add text to it every time a new message comes up.
Example of that is this:
Code:
 //Does not work?
  public void Logs(String Chat)
  {
     richTextBox1.AppendText(Chat + Environment.NewLine);
  }
It will not show anything at all.

Just want to say, I'm used to using Java. I was stuck with an API that only works for c#.
 
Hello, and welcome to Sysnative.

Can you please add the following two lines to your code just after richTextBox1.AppendText(Chat + Environment.NewLine); (they check for two separate problems, the second of which is the one I personally believe is the issue here but we'll check for them both anyway):

Code:
richTextBox1.Visible = true;
this.Show();

Landmarkwings said:
But I'm trying to add text to it every time a new message comes up.

I think this is the crux of the problem, or at least the way you're trying to do it. I'm fairly sure that the problem is that you're writing to a second RichTextBox and not the original one you meant to.

If the first line makes a second textbox visible, you're creating a duplicate RichTextBox somewhere. If the second line of code makes a whole new Form visible, you're creating a new form.



You cannot write code like Form form = new Form1(); It doesn't work - it creates an unseen second window. I think this is what is going on here. You must instead store a reference to the original form object and use that, possible with some invocation. If you're operating from a different thread and start getting "Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on" exceptions, read these two links:

.net - How to update the GUI from another thread in C#? - Stack Overflo
c# - Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on - Stack Overflo

Hope this helps.

Richard
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top