TX Text Control .NET for Windows Forms

Headers and Footers

Topic

Headers and footers repeat on every page of a section: a letterhead, a report title, "Page 3 of 12". In a Windows Forms TextControl, users add them from the ribbon or the Page Setup dialog box and type into them after activating them with the mouse. Your code adds them, fills them with text and page numbers, decides how they're activated and follows the user between header, footer and main text.

The examples are methods of Form1.cs in the application from Create Your First Windows Forms Application, called from the Click handler of a button. A section is a part of a document with its own page settings, which Sections and Page Layout introduces. For headers and footers added without a user interface, see Sections, Pages, Headers, and Footers.

How Headers and Footers Work

A HeaderFooter is a text part with its own Selection, paragraphs, tables and images. Each section has a HeaderFooterCollection in Section.HeadersAndFooters. TextControl.HeadersAndFooters is the collection of the whole document; a header or footer added to it goes to the first section, and every other section receives one connected to the previous section. Once a document has more than one section, use the collection of the section, because most collections of a HeaderFooter from the document's collection are then null.

Class diagram of the header and footer classes: TextControl, WPF.TextControl and ServerTextControl with HeadersAndFooters, the activation properties and events, HeaderFooterCollection, HeaderFooter with its collections and PageNumberField.

Each header or footer has a type from HeaderFooterType:

TypeAppears on
Header, FooterEvery page, or the odd pages if the section has an even header or footer
FirstPageHeader, FirstPageFooterThe first page of the section
EvenHeader, EvenFooterThe even pages. Adding one also creates the odd header or footer

A new section takes over the headers and footers of the previous one, connected to them so that both show the same content. HeaderFooter.ConnectedToPrevious set to false gives a section a header of its own.

Add Headers and Footers in the Ribbon

The Headers & Footers group of the Insert tab has three buttons:

  • Header edits the header of the current section, and its menu has Edit Header and Remove Header.
  • Footer does the same for the footer, with Edit Footer and Remove Footer.
  • Page Number is enabled while a header or footer is being edited. Its menu has Insert Page Number, Format Page Number... and Remove Page Number.

While a header or footer is activated, the input position is inside it. TX Text Control Words marks an activated header with a dashed line and a label such as First Header:

TX Text Control Words with the first page header of an invoice activated: a dashed line below the header, the label First Header and a selected logo image in the header.

The Headers and Footers tab of the Page Setup dialog box sets all headers and footers at once. Header, Header on first, Footer and Footer on first choose the design, Different header odd and even and Different footer odd and even add the even variants, and Distance from edge sets the positions. With Apply to set to Whole document, the Header and Footer lists offer None, All sections equal and All sections different; for the current section, None, Same as previous section and This section different.

The Page Setup dialog box on its Headers and Footers tab, with the Header list open showing None, All sections equal and All sections different, and the distance fields for header, footer and first page.

SectionFormatDialog opens this dialog box from code, and SectionFormatDialog(1) opens it on the Headers and Footers tab.

Control How Users Edit Headers and Footers

Two properties of the TextControl decide how a header or footer is entered and shown, and two events report it. The following methods set them up and move the input position into the header from code:

using System;
using System.Windows.Forms;
using TXTextControl;

public partial class Form1 : Form
{
    private void ConfigureHeaders()
    {
        // Open headers and footers with a double-click and mark them with a line
        textControl1.HeaderFooterActivationStyle = HeaderFooterActivationStyle.ActivateDoubleClick;
        textControl1.HeaderFooterFrameStyle = HeaderFooterFrameStyle.DividingLine;

        textControl1.HeaderFooterActivated += TextControl1_HeaderFooterActivated;
        textControl1.HeaderFooterDeactivated += TextControl1_HeaderFooterDeactivated;
    }

    private void TextControl1_HeaderFooterActivated(object sender, HeaderFooterEventArgs e)
    {
        Text = "Editing: " + e.HeaderFooter.Type;
    }

    private void TextControl1_HeaderFooterDeactivated(object sender, HeaderFooterEventArgs e)
    {
        Text = "Editing: main text";
    }

    private void EditHeader()
    {
        Section section = textControl1.Sections.GetItem();

        // GetItem throws an ArgumentException for a header that doesn't exist
        bool hasHeader = false;

        foreach (HeaderFooter headerFooter in section.HeadersAndFooters)
        {
            if (headerFooter.Type == HeaderFooterType.Header)
            {
                hasHeader = true;
            }
        }

        if (!hasHeader)
        {
            section.HeadersAndFooters.Add(HeaderFooterType.Header);
        }

        // Move the input position into the header
        section.HeadersAndFooters.GetItem(HeaderFooterType.Header).Activate();
    }
}

The code works as follows:

  • HeaderFooterActivationStyle is ActivateClick for a single click into a header or footer, or ActivateDoubleClick for a double-click.
  • HeaderFooterFrameStyle marks an activated header or footer with a DividingLine to the main text, a DottedFrame, a SingleFrame or NoFrame.
  • HeaderFooterActivated occurs when a header or footer receives the input position, and HeaderFooterDeactivated when it loses it. HeaderFooterEventArgs.HeaderFooter is the header or footer, and its Type tells which one. The example writes it into the title bar of the form.
  • HeaderFooter.Activate moves the input position into the header, as Edit Header does, and Deactivate moves it back to the main text. Both return true if they succeeded. Call textControl1.Focus() after Activate when a button of your own opens the header, so that the user can type into it at once.

HeaderFooterCollection.Remove removes headers and footers by type; Remove(HeaderFooterType.Header) removes the even header too, if there is one. Add and Remove also accept several values combined, such as HeaderFooterType.Header | HeaderFooterType.Footer.

Put Images and Other Content Into a Header

A header has the same collections as the main text: HeaderFooter.Images takes a logo, HeaderFooter.Tables a table and HeaderFooter.Barcodes a barcode, with the same Add methods as in Images and Charts and Barcodes. An image in a header that lies behind the text appears on every page of the section, like a watermark. The blog article Merging Multiple Records with First-Page Headers and Footers combines first page headers with a mail merge.

Next Steps