How to add text to custom cursor location in Rich Text Box in C #?

In Visual C # .NET:

How to add / add text directly to where the user cursor is in the Rich Text Box?

For example, if a user clicked a button and their cursor was somewhere in a text box with rich text, the text will be immediately added to the location of their cursor.

+5
source share
2 answers

Use property SelectedText:

textBox.SelectedText = "New text";

This will overwrite any highlighted text that they have. If you do not want you to set the property SelectionLengthto 0 first:

textBox.SelectionLength = 0;
textBox.SelectedText = "New text";
+12
source
     rtb.SelectionStart += rtb.SelectionLength;
     rtb.SelectionLength = 0;
     rtb.SelectedText = "asdf";

, "asdf" .

+5

All Articles