WinForms Rich Text Editor - How to Use the Document Iterator Object to Iterate over Document Elements
The following example shows how to use the Visitor-Iterator pattern to format document text:
- Enclose bold text in asterisks
- Return all characters without formatting
- Replace paragraph ends with newline symbols
Other document elements are skipped.
This example creates a DocumentIterator instance for the current document and calls its MoveNext method to iterate over document elements.
A Visitor pattern is implemented to process a document element. The implementation is done by calling each element's Accept method with the MyVisitor object instance as a parameter.
MyVisitor
is a custom class that descends from the DocumentVisitorBase class. It contains a method that processes DocumentText elements to enclose bold text in asterisks and return all characters without formatting. Paragraph ends are replaced with newline symbols, other document elements are skipped.