This example will show you how to generate an index for a document using TX Text Control. In an index you find keywords and on which page they are located in the text. Such an index can be easily generated automatically with a TX control.
First of all, you must give the user the ability to define a word in the text as a keyword. This is done by inserting a field in the sample (Menu Edit -> Insert Keyword).
Then when the document is set up and all the keywords have been inserted, the user should generate the index. In the sample a new form pops up with another TX Text Control that shows the index. The index is generated new every time the user opens the form, so it's always "up to date"...
So, the main thing happens when the user opens the form. Here is the Form_Load event:
Private Sub Form_Load() Dim lID As Long Dim lCurPos As Long Dim lPage As Variant Dim nTemp As Integer Dim szCur As String Dim szLast As String TXTextControl1.ResetContents TXTextControl1.LockWindowUpdate = True TXTextControl1.FontBold = True TXTextControl1.FontSize = 24 TXTextControl1.Alignment = 2 TXTextControl1.SelText = "Index" + vbCrLf + vbCrLf TXTextControl1.FontBold = False TXTextControl1.FontSize = 14 TXTextControl1.Alignment = 0 szCur = "" With Form1 ' Store the current input position lCurPos = .TXTextControl1.SelStart lID = .TXTextControl1.FieldNext(0, 0) List1.Clear ' Loop through all fields and store their text in a ListBox ' The Itemdata property is used to save the page on which the field is ' and the field ID (Page = HIWORD, ID = LOWORD) While lID <> 0 If .TXTextControl1.FieldData(lID) = 1 Then .TXTextControl1.FieldCurrent = lID .TXTextControl1.SelStart = .TXTextControl1.FieldStart lPage = .TXTextControl1.CurrentInputPosition List1.AddItem .TXTextControl1.FieldText List1.ItemData(List1.NewIndex) = lPage(0) * 65536 + .TXTextControl1.FieldCurrent End If lID = .TXTextControl1.FieldNext(lID, 0) Wend ' Restore old input position .TXTextControl1.SelStart = lCurPos ' Now loop through all list items and insert the indices For i = 0 To List1.ListCount - 1 If (UCase(Left(List1.List(i), 1)) <> szCur) Then szCur = UCase(Left(List1.List(i), 1)) TXTextControl1.FontBold = True TXTextControl1.FontSize = 18 TXTextControl1.SelText = vbCrLf + vbCrLf + szCur + vbCrLf TXTextControl1.FontBold = False TXTextControl1.FontSize = 14 End If ' Check here if the index has been inserted already ' If so, only append its page number If (szLast = List1.List(i)) Then TXTextControl1.SelText = ", " Else TXTextControl1.SelText = vbCrLf + List1.List(i) + " " szLast = List1.List(i) End If ' The page number is inserted as a field, so the user ' can click and scroll to that field nTemp = List1.ItemData(i) Mod 65536 TXTextControl1.FieldInsert Str((List1.ItemData(i) - nTemp) / 65536) TXTextControl1.FieldData(TXTextControl1.FieldCurrent) = nTemp Next i End With TXTextControl1.LockWindowUpdate = False End Sub
The TX Text Control is cleared and the window update is locked, so the user won't see all the things we do. Then the text "Index" is inserted in big letters. Now, we loop through all the fields in the TX on the main form and get their field texts, i.e. the keywords. To store the keywords we use a common list box. So, the items will be sorted automatically. The page number and the field id are also stored. The ItemData property is used for this purpose.
After we have gathered all the keywords, we start looping through the list box items and append them to the index document. A larger letter is inserted every time a keyword starts with a new letter. The current letter is stored in the variable szCur.
The last thing we need to check is if the last word we added is equal to the word we want to add. Perhaps the same keyword is used several times in the document, so we insert only the page number on which he keyword is and not the whole word again. The page numbers are inserted as fields, so the user can click on them and the main TX scrolls to the keyword. That's why we stored the field IDs earlier.