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.

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 Paragraph
- Open the Styles side bar
- Click Manage Styles
- Create a new style called "heading1"
- Apply the style to our heading
The next step is to define the Structure
In the References tab, go to the Paragraph Structure Levels group. Then find "heading1", and select Level 1 from the dropdown.
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.
Creating a TOC in C#
Of course this can be achieved programmatically as well with only a few steps. To create and apply a Paragraph
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 Table
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.
Also See
This post references the following in the documentation:
- TXText
Control. Paragraph Style Class - TXText
Control. Paragraph Format. Structure Level Property - TXText
Control. Table Of Contents Class - TXText
Control. Table Of Contents Collection Class
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
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.
Related Posts
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.
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…
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…
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…
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.