Clear a single Selection

2 posts, 0 answers
  1. William
    William avatar
    1 posts
    Member since:
    Sep 2012

    Posted 28 Sep 2012 Link to this post

    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.
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 01 Oct 2012 Link to this post

    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.

  3. DevCraft banner
Back to Top