How do I find the position of a cursor in a text box? C#

Regardless of whether any text is selected, the SelectionStart property represents the index into the text where you caret sits. So you can use String.Insert to inject some text, like this:

myTextBox.Text = myTextBox.Text.Insert(myTextBox.SelectionStart, "Hello world");

You want to check the SelectionStart property of the TextBox.


James, it's pretty inefficient that you need to replace the whole string when you only want to insert some text at the cursor position.

A better solution would be:

textBoxSt1.SelectedText = ComboBoxWildCard.SelectedItem.ToString();

When you have nothing selected, that will insert the new text at the cursor position. If you have something selected, this will replace your selected text with the text you want to insert.

I found this solution from the eggheadcafe site.


All you have to do is this:

Double click the item (button, label, whatever) that will insert text into the document at the cursor. Then type this in:

richTextBox.SelectedText = "whatevertextyouwantinserted";