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

GetCurrentWord

7 Answers 210 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Rieni De Rijke
Top achievements
Rank 1
Rieni De Rijke asked on 06 Feb 2012, 02:11 PM
I'm trying to get the word I just wrote.
I tried these methods.

private void EditorPreviewEditorKeyDown(object sender, PreviewEditorKeyEventArgs e)
{
  var word = radRichTextBox.Document.CaretPosition.GetCurrentWord();
or... var word = radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().Text;.

But none of them gives me the desired result.
Do they work in Wpf? What do they do? How could I get the word 'at the caret'?
Where can I read (wpf)documentation about this?

7 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 07 Feb 2012, 04:16 PM
Hello Rieni,

You can use DocumentPositions In order to get the last word you have typed:

DocumentPosition endPosition = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
DocumentPosition startPosition = new DocumentPosition(endPosition);
startPosition.MoveToCurrentWordStart();
this.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
this.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
string lastWord = this.radRichTextBox.Document.Selection.GetSelectedText();
this.radRichTextBox.Document.Selection.Clear();

The WPF documentation about selection is located at http://www.telerik.com/help/wpf/radrichtextbox-features-selection.html 

When it comes to the approach you have chosen - it does not work for two reasons:
1. There is no direct parallel between SpanLayoutBox and a word. For example, the text in one paragraph is split into Spans according to the formatting. After that, each Span is split into SpanLayoutBoxes at the places where you have formatting symbols (line break, interval, etc.) or digits. For example, if you have "word1" in the document, there will be 1 Span, but 2 SpanLayoutBoxes - "word" and "1" respectively. If there is different formatting within the same word, the Text of the SpanLayoutBox will also not represent the whole world.
2. The GetCurrentSpanBox() method of DocumentPosition returns the SpanLayoutBox that the position is in or the next SpanLayoutBox if the position is between two layout boxes. In your case, if you are at the end of the word, it will return the next SpanLayoutBox (which probably has an interval as Text).

I hope this helps.
Kind regards,
Martin
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Rieni De Rijke
Top achievements
Rank 1
answered on 08 Feb 2012, 03:45 PM
Thank you for your quick reply.
It works fine, but do I have to select first before I can get the last word?

And what does the radRichTextBox.Document.CaretPosition.GetCurrentWord() do?
When and where could this be used?
0
Accepted
Iva Toteva
Telerik team
answered on 09 Feb 2012, 01:20 PM
Hi Rieni,

The GetCurrentWord method of DocumentPosition returns the word that the position is in. If the caret is between two words, e.g. "first |second", it will return "second". However, if it is just after "first" ("first| second"), it will return an interval " ". Similarly, if the word is at the end of the document "last|", the current word will be "\r\n", what stands for a new pararagraph or the end of the document.

Considering the fact that you want to get the word that has just been typed, you will probably fall in this case and the GetCurrentWord method will return "\r\n". You are right though, that you can use this method, only adjust the position if needed like this:

DocumentPosition positionInDocument = new DocumentPosition(this.editor.Document.CaretPosition);
if (this.editor.Document.CaretPosition.GetCurrentSpanBox().IsFormattingSymbol)
{
    positionInDocument.MoveToPrevious();
}
MessageBox.Show(positionInDocument.GetCurrentWord());

Greetings,
Iva Toteva
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Robert
Top achievements
Rank 1
answered on 14 Feb 2012, 02:23 AM
Hi,

Is there a class method to get all words within a span?

For example, I have the following:
<telerik:Span Text="Doors and Drawers: "></telerik:Span>

When I use the following code I only get one of the words such as "Doors", "and" or "Drawers". However I want it to return "Doors and Drawers".
string currentSpanText = this.radRichTextBox1.Document.CaretPosition.GetCurrentSpanBox().Text;

Thank you very much for your time,

Rob
0
Iva Toteva
Telerik team
answered on 16 Feb 2012, 06:09 PM
Hi Rob,

If you want to obtain the text of the whole span, you can use the AssociatedSpan property of SpanLayoutBox in the following way:
string currentSpanText = this.radRichTextBox1.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan.Text;

Note that if you allow images and annotations in the document, GetCurrentSpanBox() will return null, if the caret is positioned just before an image or an annotation. Therefore, you may need to add a check for that.

Greetings,
Iva Toteva
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Robert
Top achievements
Rank 1
answered on 17 Feb 2012, 02:16 AM
Excellent that works. Thank you very much.

One last question. It seems that spans merge at runtime unless they have different properities such as font size. Is it possible to stop spans from merging even if their properties are the same?

For example:
<t:Paragraph>
<t:Span Text="Hello world! "></t:Span>
<t:Span Text="Hello Rob!"></t:Span>
</t:Paragraph>

will produce a string of "Hello World! Hello Rob!"

But if I alter a property in one of them such as font size:
<t:Paragraph>
          <t:Span Text="Hello world! "></t:Span>
          <t:Span FontSize="20" Text="Hello Rob!"></t:Span>
</t:Paragraph>

I get two strings. One is "Hello world! " and another "Hello Rob!".

I want the effect of two seperate spans / strings like the last example but I don't want to have to alter a property in order to achieve it.
Is there any way to achieve this?

Thank you again for your time.
0
Ivailo Karamanolev
Telerik team
answered on 17 Feb 2012, 04:54 PM
Hello,

Spans with equal properties cannot be prevented from merging. This is so because spans are only concerned with formatting and aren't meant to be used when preserving semantic or structural content is needed.
However, we are currently preparing a blog post specifically on storing semantic information. I'm attaching a work-in-progress version of it here, so you can have a look at it. You can also check the demo for Silverlight at ...
Let us know if you need additional information.

Regards,
Ivailo Karamanolev
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
RichTextBox
Asked by
Rieni De Rijke
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Rieni De Rijke
Top achievements
Rank 1
Iva Toteva
Telerik team
Robert
Top achievements
Rank 1
Ivailo Karamanolev
Telerik team
Share this question
or