// Assume 'textPointer' is an instance of ITextPointer pointing to some position in the text TextPointerContext elementType = textPointer.GetElementType(); if(elementType == TextPointerContext.Text) { Console.WriteLine("Current position contains text."); }
// Assume 'editor' is an instance of RichTextBox control TextPointer start = editor.Document.ContentStart; TextPointer end = editor.Document.ContentEnd; TextPointer current = start; while(current != null && current.CompareTo(end) < 0) { TextPointerContext elementType = current.GetElementType(); if(elementType == TextPointerContext.Text) { string text = current.GetTextInRun(LogicalDirection.Forward); Console.WriteLine("Text found: " + text); } current = current.GetNextContextPosition(LogicalDirection.Forward); }This code snippet scans the contents of a RichTextBox control and searches for text elements. It retrieves the text by calling the GetTextInRun method of the current text position. This example could be part of a spell checker or a text analysis tool. This method is part of the System.Windows.Documents namespace in WPF.