Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

How to build a paragraph collection

This source code snippet requires TX Text Control .NET
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 Download [20.59 KB, ZIP]
Click to enlarge

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.

  1. Public ReadOnly Property Paragraphs() As Collection
  2. Get
  3. Dim startPos As Integer = 0
  4. Dim newPos As Integer
  5.  
  6. Do
  7. newPos = TX.Find(Chr(10), startPos, _
  8. TXTextControl.FindOptions.NoMessageBox)
  9.  
  10. If newPos = -1 Then Exit Do
  11.  
  12. Dim newPar As New Paragraph
  13.  
  14. newPar.Start = startPos
  15. newPar.Length = newPos - startPos
  16.  
  17. TX.Select(startPos, newPos - startPos)
  18.  
  19. If newPos <> startPos Then
  20. newPar.Text = TX.Selection.Text
  21. Else
  22. newPar.Text = ""
  23. End If
  24.  
  25. newPar.Style = TX.Selection.FormattingStyle
  26. newPar.Page = TX.InputPosition.Page
  27.  
  28. m_par.Add(newPar)
  29. startPos = newPos + 1
  30. Loop
  31.  
  32. Return m_par
  33. End Get
  34. 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.

  1. Dim myParagraphs As New Paragraphs
  2. myParagraphs.TX = TextControl1
  3.  
  4. For Each par As Paragraph In myParagraphs.Paragraphs
  5. debug.WriteLine(par.Text)
  6. [...]
  7. Next

The sample requires Visual Studio 2003 and at least a TX Text Control .NET 11.0 trial version.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place