views:

36

answers:

2

How can you loop through all the "words" (spaces deliminate words) in an RTB (WPF Control) to see which ones are italicized?

thanks

A: 

Well, your task seems to be a quite complicated one.

The contents of a RichTextBox is a FlowDocument which can be found at the property Document. The FlowDocument, in turn, consists of several Blocks.

Each of the Blocks can be a Paragraph, a Section, a Table etc. You'll need to analyze each of them separately.

For the Paragraph, it consists of several Inlines, each of them can be a Span, which in turn may be an Italic. The Italic represents italicized text. The Italic can, in turn, have other inlines, containing other Spans (for example, Hyperlinks, which you may or may not want to include into your result).

You you basically need to traverse all the structure recursively and peek the text from your Italics. A special case may be the words where only a part is italicized, you'll need to have a strategy for them.

I am unaware of any easier methods to achieve what you want. HTH.

Edit:
Perhaps an easier alternate solution would be to traverse all the text using TextPointer from the beginning (richTextBox.Document.ContentStart), switching to the next position with position.GetNextContextPosition(LogicalDirection.Forward), and testing if your current position is inside an Italic using position.Parent. You should however care that Italic can be a non-immediate parent, so you'll perhaps need to traverse several parents upwards. Disclaimer: I did never try this idea in my code.

Vlad
I used your basic idea of checking each for to see if it is italic or not
mike
TextPointer tp = RTB.Document.ContentStart; TextRange word = WordBreaker.GetWordRange(tp); while (word.End.GetNextInsertionPosition(LogicalDirection.Forward) != null) { if (word.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "Italic") { } word = WordBreaker.GetWordRange(word.End.GetNextInsertionPosition(LogicalDirection.Forward)); } }
mike
with WordBreaker class from http://blogs.msdn.com/b/prajakta/archive/2006/11/01/navigate-words-in-richtextbox.aspx
mike
A: 
TextPointer tp = RTB.Document.ContentStart;
TextRange word = WordBreaker.GetWordRange(tp);

   while (word.End.GetNextInsertionPosition(LogicalDirection.Forward) != null)
        {
            if (word.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "Italic")
            {

            }
            word = WordBreaker.GetWordRange(word.End.GetNextInsertionPosition(LogicalDirection.Forward));  
        }
    }

with WordBreaker class from http://blogs.msdn.com/b/prajakta/archive/2006/11/01/navigate-words-in-richtextbox.aspx

mike