The best way to accomplish this is to use the TextBox.Text.Insert(int indexSelectionStart, string text). What this method does is insert text into the TextBox at the index you specify - it uses string string.insert(int startIndex, string value) as TextBox.Text is a string which we are going to insert text into at a specific point. You wish to insert text where the cursor/selector is, and to find that index, we can use TextBox.SelectionStart.
Let's say that your TextBox is named textBox1.
This is what the code may look like, presuming that the text you wish to insert is stored in the string named strInsert.
string strInsert = "I am inserting this text.";
textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, strInsert);
I realize this is an old post but i hope this collection of methods for TextBox will help others struggling with manipulating this control.
A much easier way would be to use the
Paste
method:I've done this using .NET 4.0
The best way to accomplish this is to use the TextBox.Text.Insert(int indexSelectionStart, string text). What this method does is insert text into the TextBox at the index you specify - it uses
string string.insert(int startIndex, string value)
as TextBox.Text is a string which we are going to insert text into at a specific point. You wish to insert text where the cursor/selector is, and to find that index, we can use TextBox.SelectionStart.Let's say that your TextBox is named textBox1. This is what the code may look like, presuming that the text you wish to insert is stored in the string named strInsert.
try this code: