-6

How to edit data in rich text box. I'm using like a button to click. When I click it, more data will be added to rich text box. But when I click it, it deleted the current data from box and replace with the new one. How do I make it so the old data and newly added data will stay in place ?

3 Answers3

1

Use .AppendText(String) instead of .Text.

That said, it's going to get costly, and you can easily run into OutOfMemoryExceptions if you do this too much. I suggest following some of the advice here.

Community
  • 1
  • 1
CDove
  • 1,940
  • 10
  • 19
0

Try This

string Text = "here your text to append";

richtext.Text += " " + Text;
0

Assumming at the moment you are simply setting the .Text value equal to you new value, as below:

richTextBox1.Text = "Some More Text";

When you do this you are changing the string value to you new value only. What you need to do is append the string you wish to add to end of the current string. You can do this as follows:

private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.Text += "Some More Text";
}

This will add the text Some More Text to the end of you current value when the button is clicked. If you wanted to append it to the start, or in some other more complex way you should access the existing string using richTextBox1.Text and then concatenate it in the position you want. Such as:

private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.Text = richTextBox1.Text + "Some More Text";
}

The above will append your new string to the start of your Rich Text rather than the end.

Dan Gardner
  • 1,069
  • 2
  • 13
  • 29
  • AppendText does exactly this but avoids the high overhead of having to re-parse all the the existing RTF when its all overwritten on the assignment to .Text – Alex K. Jan 13 '17 at 14:30
  • @AlexK. That's good to know, it's not a method I've ever come across before. Thanks for the information! – Dan Gardner Jan 13 '17 at 14:55
  • @AlexK. Does it only allow you to append text to the end of the string is it possible to select the position to insert? I'm going to take a look at the docs now – Dan Gardner Jan 13 '17 at 14:56
  • It always appends to the end iirc – Alex K. Jan 13 '17 at 15:04