Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add text to the location of a user's cursor in a Rich Text Box in C#?

Tags:

c#

richtextbox

In Visual C#.NET:

How do I add/append text directly to where the user's cursor is in a Rich Text Box?

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

like image 953
Alper Avatar asked Jun 18 '11 02:06

Alper


People also ask

How to set the cursor position in TextBox in c#?

To position the cursor at the beginning of the contents of a TextBox control, call the Select method and specify the selection start position of 0, and a selection length of 0.

How do you add to a rich TextBox?

Under Insert controls, click Rich Text Box. In the Rich Text Box Binding dialog box, select the field in which you want to store rich text box data, and then click OK.

How to insert text into the textarea at the current cursor position in angular?

First, get the current position of cursor with the help of property named as selectionStart on textarea/inputbox. To insert the text at the given position we will use slice function to break the string into two parts and then we will append both parts to the text(text_to_insert) in front and end of the text.


2 Answers

Use the SelectedText property:

textBox.SelectedText = "New text";

This will overwrite any selected text they have though. If you don't want that you can first set the SelectionLength property to 0:

textBox.SelectionLength = 0;
textBox.SelectedText = "New text";
like image 134
kabuko Avatar answered Sep 19 '22 05:09

kabuko


     rtb.SelectionStart += rtb.SelectionLength;
     rtb.SelectionLength = 0;
     rtb.SelectedText = "asdf";

This moves the cursor just past the end of the current selection, then adds "asdf" at the end.

like image 39
agent-j Avatar answered Sep 22 '22 05:09

agent-j