

TX Text Control .NET for Windows Forms offers a Line and a Chars collection to access the text in TX Text Control .NET for Windows Forms. 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 PropertyWe 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)
[...]
NextThe sample requires Visual Studio 2003 and at least a TX Text Control .NET for Windows Forms 11.0 trial version.