| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| Author: | TX Text Control Support Department |
| Language: | Visual Basic |
| Version: | 1.0 |
| Released: | June 06, 2005 |
| Last modified: | January 11, 2008 |
| Requirements: | TX Text Control .NET with Visual Basic .NET |
| Download code: | tx_sample_dotnet_par.zip |

TX Text Control .NET offers a Line and a Chars collection to access the text in TX Text Control .NET. This makes sense as the Selection class uses the character index, which is returned by these collections. Anyway, sometimes it is required to have a collection that contains all paragraphs. This sample shows how to build a paragraph collection to iterate through all paragraphs in a document.
First, we created the Paragraph Class with the properties Text, Start, Length, Style and Page. The last two are particularly interesting for further processing. For example in order to create a a table of contents.
Secondly, we need a collection, which holds the Paragraph objects. The class Paragraphs contains a collection (in fact an ArrayList). The read only Paragraphs property returns this collection with all Paragraph objects.
Public ReadOnly Property Paragraphs() As Collection Get Dim startPos As Integer = 0 Dim newPos As Integer Do newPos = TX.Find(Chr(10), startPos, _ TXTextControl.FindOptions.NoMessageBox) If newPos = -1 Then Exit Do Dim newPar As New Paragraph newPar.Start = startPos newPar.Length = newPos - startPos TX.Select(startPos, newPos - startPos) If newPos <> startPos Then newPar.Text = TX.Selection.Text Else newPar.Text = "" End If newPar.Style = TX.Selection.FormattingStyle newPar.Page = TX.InputPosition.Page m_par.Add(newPar) startPos = newPos + 1 Loop Return m_par End Get End Property
We utilize the Find method to search for the next carriage return control character. The text between two carriage returns is the text of the new Paragraph object which is stored in the collection. Additionally, we store the start position and the length of the text for later communication with the Selection class.
There are two different interpretations for a paragraph. In this sample, a valid paragraph must end with a carriage return. Additionally, an empty paragraph (just a carriage return) is a valid paragraph with no text. This can be changed, if required.
In this sample, we use the paragraph collection to display a simple document summary. We can iterate through all paragraphs using a For Each statement.
Dim myParagraphs As New Paragraphs myParagraphs.TX = TextControl1 For Each par As Paragraph In myParagraphs.Paragraphs debug.WriteLine(par.Text) [...] Next
The sample requires Visual Studio 2003 and at least a TX Text Control .NET 11.0 trial version.