mxy
mxy

Reputation: 9

C#, messages supposed to be among each other

i created a windows forms-app with visual studio and im programming in c#.

I have one textbox, one richtextbox and one button. If im writing something in the textbox and press the button, the text from the textbox is shown in the richtextbox (just like a simple chat). My problem is, that if i already wrote something and add something new, the old text in the richtextbox gets replaced by the new one. I want that everything i wrote is among each other. My source code:

string message = textbox1.Text;
richTextBox1.Text = "Name:" + message;

Upvotes: 0

Views: 64

Answers (2)

James Hill
James Hill

Reputation: 61812

Using the equals operator replaces the value in the .Text property. If you'd like to append text, use the += operator:

string message = textbox1.Text;
richTextBox1.Text += "Name:" + message;

Alternatively, you can skip the shorthand and say:

string message = textbox1.Text;
richTextBox1.Text = richTextBox1.Text + "Name:" + message;

Or, use AppendText():

richTextBox1.AppendText(message)

Upvotes: 2

Caius Jard
Caius Jard

Reputation: 74605

Of course; you set the Text of the richtextbox to a new text each time you access it

Consider using richTextBox1.AppendText(message) instead

Upvotes: 2

Related Questions