Delete Selected Text in Richtextbox

c#, richtextbox, winforms

Solution

Just do this:

private void button1_Click(object sender, EventArgs e)
{  
   richTextBox1.SelectedText = "";
}

Your code doesn't work because the string is `immutable`, you can't change the `richTextBox1.SelectedText` that way. All the methods (`Replace`, `Insert`, ...) performed on a `string` will create a new `string`. This new string will be used to initialize your string variable if you need.

Problem

I have a `richtextbox` and i want to `delete` not `cut` the selected when the user presses a button. I have used ``` private void button1_Click(object sender, EventArgs e) { SendKeys.Send("DELETE"); } ``` This works but i want to know another way to do it. I have tried ``` private void button1_Click(object sender, EventArgs e) { richTextBox1.SelectedText.Replace(richTextBox1.SelectedText, ""); } ``` This doesn't perform any action. Pls what can i do?

Original source