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

GetSelectedText is empty

2 Answers 50 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Sean
Top achievements
Rank 1
Sean asked on 17 Sep 2013, 09:21 PM
I would like to get the selected text from the rich text box.

When I drag a selection in the ui and then query the selected text I get an empty string:
var thisIsEmpty = rtb.Document.Selection.GetSelectedText()


If I use the Document.CaretPosition to set the selection to the beginning and end of the word, the GetSelectedText() returns that selected word. However, that is just the word under the caret - not necessarily all of the selected words.
MainMessageBodyTextBox.Document.Selection.SetSelectionStart(cpos);
cpos.MoveToCurrentWordEnd();
MainMessageBodyTextBox.Document.Selection.AddSelectionEnd(cpos);
text = MainMessageBodyTextBox.Document.Selection.GetSelectedText();


My questions:
  • why is the initial selection empty when dragged from the ui (my document is ReadOnly, but changing that didn't seem to make a difference)
  • how can I retrieve all of the selected words from the Selection (the words will be contiguous)

thank you


2 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 20 Sep 2013, 02:12 PM
Hello Sean,

I was unable to replicate inconsistent behavior of the GetSelectedText() method. Could you please share with us what version of RadControls for Silverlight you are using?

Generally, once you've retrieved the selection you can split the string in order to obtain all separate words in it.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Sean
Top achievements
Rank 1
answered on 22 Sep 2013, 08:49 PM
Hello Petya,

we are using version 2013.2.611.1050.

However, I have just found the problem. There was an event handler firing (mouse down) in our code that had moved the document CaretPosition. On the mouse up, the document was still reporting a selection, but the caret move appears to have set the selected text to an empty string.

This was my misunderstanding of the document selection vs the selection with caret interaction. Sorry for the work caused.

thank you, Sean
Tags
RichTextBox
Asked by
Sean
Top achievements
Rank 1
Answers by
Petya
Telerik team
Sean
Top achievements
Rank 1
Share this question
or