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

Adding headers and footers to documents

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
Download code:tx_sample_ocx_page_number.zip Download [2.17 KB, 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:

Visual Basic

Have a look at the headers example and add a menu item. Add the following code to the click event:

  1. Dim fieldNo
  2. Dim noPages
  3.  
  4. Private Sub Form_Load()
  5.  
  6. TXTextControl1.HeaderFooter = txHeader
  7. TXTextControl1.HeaderFooterSelect txHeader
  8. TXTextControl1.Text = "Page: of"
  9. TXTextControl1.SelStart = Len("Page: ")
  10. TXTextControl1.FieldInsert ("")
  11. TXTextControl1.FieldType(TXTextControl1.FieldCurrent) = txFieldPageNumber
  12. TXTextControl1.SelStart = Len("Page: x of ")
  13. TXTextControl1.FieldInsert ""
  14. fieldNo = TXTextControl1.FieldCurrent
  15. noPages = TXTextControl1.CurrentPages
  16. TXTextControl1.FieldText = Str(noPages)
  17. TXTextControl1.HeaderFooterSelect 0
  18.  
  19. 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:

  1. Private Sub TXTextControl1_Change()
  2.  
  3. If (noPages <> -1) And (TXTextControl1.CurrentPages <> noPages) Then
  4. noPages = TXTextControl1.CurrentPages
  5. TXTextControl1.HeaderFooterSelect txHeader
  6. TXTextControl1.FieldCurrent = fieldNo
  7. TXTextControl1.FieldText = Str(noPages)
  8. TXTextControl1.HeaderFooterSelect 0
  9. TXTextControl1.Refresh
  10. End If
  11.  
  12. End Sub

Delphi

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!

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