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 TX Text Control .NET for Windows Forms
TXTextControl Namespace
ParagraphStyle Class
The ParagraphStyle class defines a formatting style for paragraphs.
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 TX Text Control .NET for Windows Forms
TXTextControl Namespace
ParagraphFormat Class
StructureLevel Property
Gets or sets the structure level of a paragraph in the document.
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 TX Text Control .NET for Windows Forms
TXTextControl Namespace
ParagraphStyle Class
The ParagraphStyle class defines a formatting style for paragraphs.
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";
view raw test.cs hosted with ❤ by GitHub

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";
view raw test.cs hosted with ❤ by GitHub

To add a new table of contents to the document, the object must be created and added to the TableOfContentsCollection TX Text Control .NET for Windows Forms
TXTextControl Namespace
TableOfContentsCollection Class
Contains all tables of contents in a certain part of the document.
.

var toc = new TableOfContents(1, 2);
toc.ID = id;
toc.Title = "Table of Contents";
id++;
textControl1.TablesOfContents.Add(toc);
view raw test.cs hosted with ❤ by GitHub

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.