Reputation: 9
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
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
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