This is a migrated thread and some comments may be shown as answers.

Clear a single Selection

1 Answer 60 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 28 Sep 2012, 01:40 PM
Hi.
I had a need to get the position of the cursor on a particular line in the RadRichTextBox. To do that, I added a selection from the start of the current line to the caret position, and got the length of the selected text, as follows:

Dim

 

 

iCol As Integer = 1

 

 

 

Dim oCaretPos As DocumentPosition = oRadRTB.Document.CaretPosition

 

 

 

Dim oStartPos As DocumentPosition = New DocumentPosition(oCaretPos)

 

oStartPos.MoveToCurrentLineStart()

oRadRTB.Document.Selection.AddSelectionStart(oStartPos)

oRadRTB.Document.Selection.AddSelectionEnd(oCaretPos)

iCol = oRadRTB.Document.Selection.GetSelectedText().Length + 1


Once I get that value (iCol), I want to clear the selection I just added. When I call Document.Selection.Clear(), it clears all selections. What this means is that if the user selectes text, that that selection is cleared along with my programmatic selection.

Is there a way to clear my programmatic selection without clearing the user's selection? Can I iterate through selections, for instance, and remove individual ones?

Thank you.

1 Answer, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 01 Oct 2012, 08:40 AM
Hello William,

You can create a new instance of the DocumentSelection class and use it instead of changing the visible selection (that is a property of the document).

Here is an example:

DocumentSelection selection = new DocumentSelection(this.radRichTextBox.Document);
DocumentPosition start = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
start.MoveToCurrentLineStart();
selection.SetSelectionStart(start);
DocumentPosition end = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
selection.AddSelectionEnd(end);
MessageBox.Show(selection.GetSelectedText());
selection.Clear();

In VB and your variable names, that would be:
Dim selection As New DocumentSelection(Me.oRadRTB.Document)
Dim oStartPos As New DocumentPosition(Me.oRadRTB.Document.CaretPosition)
oStartPos.MoveToCurrentLineStart()
selection.SetSelectionStart(oStartPos)
Dim oCaretPos As New DocumentPosition(Me.oRadRTB.Document.CaretPosition)
selection.AddSelectionEnd(oCaretPos)
MessageBox.Show(selection.GetSelectedText())
selection.Clear()

I hope this helps.

Regards,
Iva Toteva
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

Tags
RichTextBox
Asked by
William
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or