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

Index generation with TX Text Control ActiveX

This source code snippet requires TX Text Control ActiveX
Author:TX Text Control Support Department
Language:Visual Basic
Version:1.1
Released:March 20, 2001
Last modified:January 11, 2008
Requirements:TX Text Control ActiveX with Visual Basic 6.0

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:

  1. Private Sub Form_Load()
  2.  
  3. Dim lID As Long
  4. Dim lCurPos As Long
  5. Dim lPage As Variant
  6. Dim nTemp As Integer
  7. Dim szCur As String
  8. Dim szLast As String
  9.  
  10. TXTextControl1.ResetContents
  11. TXTextControl1.LockWindowUpdate = True
  12.  
  13. TXTextControl1.FontBold = True
  14. TXTextControl1.FontSize = 24
  15. TXTextControl1.Alignment = 2
  16. TXTextControl1.SelText = "Index" + vbCrLf + vbCrLf
  17. TXTextControl1.FontBold = False
  18. TXTextControl1.FontSize = 14
  19. TXTextControl1.Alignment = 0
  20.  
  21. szCur = ""
  22.  
  23. With Form1
  24.  
  25. ' Store the current input position
  26. lCurPos = .TXTextControl1.SelStart
  27.  
  28. lID = .TXTextControl1.FieldNext(0, 0)
  29. List1.Clear
  30.  
  31. ' Loop through all fields and store their text in a ListBox
  32. ' The Itemdata property is used to save the page on which the field is
  33. ' and the field ID (Page = HIWORD, ID = LOWORD)
  34. While lID <> 0
  35. If .TXTextControl1.FieldData(lID) = 1 Then
  36. .TXTextControl1.FieldCurrent = lID
  37. .TXTextControl1.SelStart = .TXTextControl1.FieldStart
  38. lPage = .TXTextControl1.CurrentInputPosition
  39. List1.AddItem .TXTextControl1.FieldText
  40. List1.ItemData(List1.NewIndex) = lPage(0) * 65536 + .TXTextControl1.FieldCurrent
  41. End If
  42.  
  43. lID = .TXTextControl1.FieldNext(lID, 0)
  44. Wend
  45.  
  46. ' Restore old input position
  47. .TXTextControl1.SelStart = lCurPos
  48.  
  49. ' Now loop through all list items and insert the indices
  50. For i = 0 To List1.ListCount - 1
  51.  
  52. If (UCase(Left(List1.List(i), 1)) <> szCur) Then
  53. szCur = UCase(Left(List1.List(i), 1))
  54. TXTextControl1.FontBold = True
  55. TXTextControl1.FontSize = 18
  56. TXTextControl1.SelText = vbCrLf + vbCrLf + szCur + vbCrLf
  57. TXTextControl1.FontBold = False
  58. TXTextControl1.FontSize = 14
  59. End If
  60.  
  61. ' Check here if the index has been inserted already
  62. ' If so, only append its page number
  63. If (szLast = List1.List(i)) Then
  64. TXTextControl1.SelText = ", "
  65. Else
  66. TXTextControl1.SelText = vbCrLf + List1.List(i) + " "
  67. szLast = List1.List(i)
  68. End If
  69.  
  70. ' The page number is inserted as a field, so the user
  71. ' can click and scroll to that field
  72. nTemp = List1.ItemData(i) Mod 65536
  73. TXTextControl1.FieldInsert Str((List1.ItemData(i) - nTemp) / 65536)
  74. TXTextControl1.FieldData(TXTextControl1.FieldCurrent) = nTemp
  75.  
  76. Next i
  77.  
  78. End With
  79.  
  80. TXTextControl1.LockWindowUpdate = False
  81.  
  82. 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.

External verification page for ISO 9000:2000 certificate
ISO 9001:2000
certified