Products Technologies Demo Docs Blog Support Company

Create a Table of Contents in Windows Forms using C#

This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents. For example if you write a thesis or paper with different chapters. It also gives a good overview of the structure of the document.

Create a Table of Contents in Windows Forms using C#

Using TX Text Control .NET for Windows Forms, a table of contents can be easily created using very simple steps.

Creating a TOC using the Ribbon

First, we have to understand the concept of styles. A style can be used to apply several formatting options at once to a paragraph or part of text. This could be font settings such as family or size and other attributes such as bold, italic etc. Now we want to define a unique ParagraphStyle for the headings of each chapter. The headings will be the elements listed in our TOC.

  1. Open the Styles side bar
  2. Click Manage Styles
  3. Create a new style called "heading1"
  4. Apply the style to our heading

The next step is to define the StructureLevel for different parts of the document. For example the body of the document has level 0. The table of contents then consists of the levels that can be chosen in the References tab of the ribbon.

In the References tab, go to the Paragraph Structure Levels group. Then find "heading1", and select Level 1 from the dropdown.

Apply structure level

Now go to the very beginning of your document and choose Insert from the Table of Contents group. Then choose Level 1 for the minimum and maximum structure level and confirm with Ok. Voila! A table of contents consisting of the chosen headings is generated automatically.

Add TOC

Creating a TOC in C#

Of course this can be achieved programmatically as well with only a few steps. To create and apply a ParagraphStyle to a text selection see the following snippet.

TXTextControl.ParagraphStyle par = new TXTextControl.ParagraphStyle("heading1");
par.Bold = true;
par.FontSize = 400;
par.FontName = "Arial";

textControl1.ParagraphStyles.Add(par);

textControl1.Selection.FormattingStyle = "heading1";

The advantage is that fewer steps are required, because the structure level can already be defined in the style itself. The complete code looks like this:

TXTextControl.ParagraphStyle par = new TXTextControl.ParagraphStyle("heading1");
par.Bold = true;
par.FontSize = 400;
par.FontName = "Arial";
par.ParagraphFormat.StructureLevel = 1;

textControl1.ParagraphStyles.Add(par);

textControl1.Selection.FormattingStyle = "heading1";

To add a new table of contents to the document, the object must be created and added to the TableOfContentsCollection.

var toc = new TableOfContents(1, 2);
toc.ID = id;
toc.Title = "Table of Contents";
id++;
textControl1.TablesOfContents.Add(toc);

What happens during the creation is that the document is searched for all paragraphs with the specified structure levels, generates the table of contents with or without page numbers and adds it at the current text input position.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.ParagraphStyle Class
  • TXTextControl.ParagraphFormat.StructureLevel Property
  • TXTextControl.TableOfContents Class
  • TXTextControl.TableOfContentsCollection Class

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Windows Forms

Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.

See Windows Forms products

Related Posts

ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


.NETSampleTable of Contents

Creating a Table of Contents (TOC) Using TX Text Control

An often requested feature of TX Text Control is a dynamically created table of contents (TOC) like offered in MS Word. Based on the style interface of TX Text Control, this is possible and not…


ASP.NETWindows FormsWPF

Combining MailMerge and Table of Contents

MailMerge is used to merge data into templates from JSON data or IEnumerable objects. This article explains how to combine table of contents with dynamically generated documents using the…


Windows FormsList.NET

Two Ways to Restart Numbered Lists in TX Text Control

In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…


ASP.NETWindows FormsWPF

Creating Table of Contents without Stylesheets

Table of contents are created automatically based on used stylesheets for the different structure levels. This sample shows how to convert paragraphs to stylesheets to insert a table of contents.