# 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.

- **Author:** Malin Teegen
- **Published:** 2023-01-23
- **Modified:** 2026-07-17
- **Description:** 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.
- **3 min read** (447 words)
- **Tags:**
  - Windows Forms
  - WPF
  - .NET Framework
  - TOC
  - .NET
  - Sample
  - Table of Contents
- **Web URL:** https://www.textcontrol.com/blog/2023/01/23/create-toc-in-windows-forms/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/01/23/create-toc-in-windows-forms/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/01/23/create-toc-in-windows-forms/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.WindowsForms.createTOC

---

Using [TX Text Control .NET for Windows Forms](https://www.textcontrol.com/product/tx-text-control-dotnet-winforms/), 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](https://s1-www.textcontrol.com/assets/dist/blog/2023/01/23/a/assets/screenshot.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/01/23/a/assets/screenshot2.webp "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.

---

## About Malin Teegen

Malin Teegen is Head of Strategic Accounts and Marketing at Text Control in Bremen, Germany. She specializes in business development and the strategic expansion of customer relationships in the field of digital document processing. With a master's degree in marine biology from the University of Bremen and a certification in online marketing from Hamburg Media School, she combines analytical thinking with entrepreneurial vision. Her diverse experience in sales, online marketing, and digital journalism highlights her commitment to digital transformation and sustainable growth.

- [LinkedIn](https://www.linkedin.com/in/malin-teegen-32ba91255/)

---

## Related Posts

- [Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub](https://www.textcontrol.com/blog/2023/01/08/official-tx-text-control-net-sample-applications-are-now-hosted-on-github/llms.txt)
- [Creating a Table of Contents (TOC) Using TX Text Control](https://www.textcontrol.com/blog/2009/02/02/creating-a-table-of-contents-toc-using-tx-text-control/llms.txt)
- [Combining MailMerge and Table of Contents](https://www.textcontrol.com/blog/2022/03/22/combining-mailmerge-and-table-of-contents/llms.txt)
- [Two Ways to Restart Numbered Lists in TX Text Control](https://www.textcontrol.com/blog/2021/11/03/two-ways-to-restart-numbered-lists/llms.txt)
- [Creating Table of Contents without Stylesheets](https://www.textcontrol.com/blog/2020/12/15/creating-table-of-contents-without-stylesheets/llms.txt)
- [Using Custom Document Properties to Store Additional Document Information](https://www.textcontrol.com/blog/2017/02/09/using-custom-document-properties-to-store-additional-document-information/llms.txt)
