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

| 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 |
| Download code: | tx_sample_ocx_page_number.zip |
This sample application originally appeared in the TX Text Control Newsletter - why not sign up today and receive sample code like this every week, directly to your Inbox.
As of version 7 of TX Text Control the possibility of adding headers and footers to your documents has been implemented. A new kind of marked text field has also been added - the page number field. It is inserted in a header or footer and automatically displays the current page number. Some users mentioned that they would prefer a footer that also displays the total number of pages ("Page X of Y"), but they don't know how to realize that because there is no special field.
The following example shows how to display the total number of pages in your footer:
Have a look at the headers example and add a menu item. Add the following code to the click event:
Dim fieldNo Dim noPages Private Sub Form_Load() TXTextControl1.HeaderFooter = txHeader TXTextControl1.HeaderFooterSelect txHeader TXTextControl1.Text = "Page: of" TXTextControl1.SelStart = Len("Page: ") TXTextControl1.FieldInsert ("") TXTextControl1.FieldType(TXTextControl1.FieldCurrent) = txFieldPageNumber TXTextControl1.SelStart = Len("Page: x of ") TXTextControl1.FieldInsert "" fieldNo = TXTextControl1.FieldCurrent noPages = TXTextControl1.CurrentPages TXTextControl1.FieldText = Str(noPages) TXTextControl1.HeaderFooterSelect 0 End Sub
Add two global variables called noPages and fieldNo. Initialize noPages with -1.
Finally, add the following code to the TX's OnChange event:
Private Sub TXTextControl1_Change() If (noPages <> -1) And (TXTextControl1.CurrentPages <> noPages) Then noPages = TXTextControl1.CurrentPages TXTextControl1.HeaderFooterSelect txHeader TXTextControl1.FieldCurrent = fieldNo TXTextControl1.FieldText = Str(noPages) TXTextControl1.HeaderFooterSelect 0 TXTextControl1.Refresh End If End Sub
The code above is very short and as the Delphi Headers example matches the VB one almost word-by-word, you should be able to port the above code to Delphi on your own.
What does the code do?
First of all, you need to insert a header. The code assumes that a header has already been inserted, so there might be some need for error checking. Also, some users might want to add the "page x of y" line to the footer and not to the header. Simply replace txHeader with txFooter then. Now let's take a look at the page number menu handler:
The first line set the text of the header to "Page: of ". Then a page number field is inserted between "Page: " and " Of". Finally, a usual field is inserted at the end of the line. Its text is set to the number of currently inserted pages. Now, when the contents of the TX change, we need to check if the number of pages has changed as well. This is done in the OnChange handler. If the number of pages is not equal to our variable noPages, a page has just been inserted. So, change the field in the header and update noPages. And that's all there is to it!