Table of Contents Example for WinForms

Nevron NOV RichTextEditor for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms RichTextEditor examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Table of Contents Example for WinForms Description

This example demonstrates how to programmatically create and add a table of contents block to a document.


Table of Contents Example

C# Table of Contents Example Code for Nevron RichTextEditor for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NTableOfContentsExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			// Create the rich text
			NRichTextViewWithRibbon richTextWithRibbon = new NRichTextViewWithRibbon();
			m_RichText = richTextWithRibbon.View;
			m_RichText.AcceptsTab = true;
			m_RichText.Content.Sections.Clear();

			// Populate the rich text
			PopulateRichText();

			return richTextWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			return null;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to programmatically create and add a table of contents block to a document.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Rich Text Table Of Contents", "https://www.nevron.com/docs/openvision/rich-text-table-of-contents.html"), ("Rich Text Paragraph Styles", "https://www.nevron.com/docs/openvision/rich-text-paragraph-styles.html"), ("Rich Text Field Inlines", "https://www.nevron.com/docs/openvision/rich-text-field-inlines.html"), }; } private void PopulateRichText() { // Get references to the heading styles NDocumentBlock documentBlock = m_RichText.Content; NRichTextStyle heading1Style = documentBlock.Styles.FindStyleByTypeAndId(ENRichTextStyleType.Paragraph, "Heading1"); NRichTextStyle heading2Style = documentBlock.Styles.FindStyleByTypeAndId(ENRichTextStyleType.Paragraph, "Heading2"); NSection section = new NSection(); m_RichText.Content.Sections.Add(section); // Add a table of contents block NTableOfContentsBlock tableOfContents = new NTableOfContentsBlock(); section.Blocks.Add(tableOfContents); // Create chapter 1 NParagraph paragraph = new NParagraph("Chapter 1"); section.Blocks.Add(paragraph); heading1Style.Apply(paragraph); paragraph = new NParagraph("Topic 1.1"); section.Blocks.Add(paragraph); heading2Style.Apply(paragraph); AddParagraphs(section, "This is a sample paragraph from the first topic of chapter one.", 20); paragraph = new NParagraph("Topic 1.2"); section.Blocks.Add(paragraph); heading2Style.Apply(paragraph); AddParagraphs(section, "This is a sample paragraph from the second topic of chapter one.", 20); // Create chapter 2 paragraph = new NParagraph("Chapter 2"); section.Blocks.Add(paragraph); heading1Style.Apply(paragraph); paragraph = new NParagraph("Topic 2.1"); section.Blocks.Add(paragraph); heading2Style.Apply(paragraph); AddParagraphs(section, "This is a sample paragraph from the first topic of chapter two.", 20); paragraph = new NParagraph("Topic 2.2"); section.Blocks.Add(paragraph); heading2Style.Apply(paragraph); AddParagraphs(section, "This is a sample paragraph from the second topic of chapter two.", 20); // Update the table of contents m_RichText.Document.Evaluate(); tableOfContents.Update(); } #endregion #region Implementation private void AddParagraphs(NSection section, string text, int count) { // Duplicate the given text 5 times text = String.Join(" ", new string[] { text, text, text, text, text }); // Create the given number of paragraphs with the text for (int i = 0; i < count; i++) { section.Blocks.Add(new NParagraph(text)); } } #endregion #region Event Handlers #endregion #region Fields private NRichTextView m_RichText; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Table of Contents Example for WinForms Solution

VB.NET Table of Contents Example Code for Nevron RichTextEditor for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NTableOfContentsExample"

        ' Create the NOV example and its controls
        Dim exampleContent As NWidget = CreateExampleContent()
        Dim exampleControls As NWidget = CreateExampleControls()
        If Not exampleControls Is Nothing Then
            exampleControls.PreferredWidth = 300
        End If

        ' Place the example and its controls in a pair box
        Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
        pairBox.FillMode = ENStackFillMode.First
        pairBox.FitMode = ENStackFitMode.First
        pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

        ' Host the example in the form
        Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create the rich text
            Dim richTextWithRibbon As NRichTextViewWithRibbon = New NRichTextViewWithRibbon()
            m_RichText = richTextWithRibbon.View
            m_RichText.AcceptsTab = True
            m_RichText.Content.Sections.Clear()

            ' Populate the rich text
            PopulateRichText()

            Return richTextWithRibbon
        End Function
        Public Function CreateExampleControls() As NWidget
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to programmatically create and add a table of contents block to a document.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Rich Text Table Of Contents", "https://www.nevron.com/docs/openvision/rich-text-table-of-contents.html"), ("Rich Text Paragraph Styles", "https://www.nevron.com/docs/openvision/rich-text-paragraph-styles.html"), ("Rich Text Field Inlines", "https://www.nevron.com/docs/openvision/rich-text-field-inlines.html")} End Function Private Sub PopulateRichText() ' Get references to the heading styles Dim documentBlock = m_RichText.Content Dim heading1Style = documentBlock.Styles.FindStyleByTypeAndId(ENRichTextStyleType.Paragraph, "Heading1") Dim heading2Style = documentBlock.Styles.FindStyleByTypeAndId(ENRichTextStyleType.Paragraph, "Heading2") Dim section As NSection = New NSection() m_RichText.Content.Sections.Add(section) ' Add a table of contents block Dim tableOfContents As NTableOfContentsBlock = New NTableOfContentsBlock() section.Blocks.Add(tableOfContents) ' Create chapter 1 Dim paragraph As NParagraph = New NParagraph("Chapter 1") section.Blocks.Add(paragraph) heading1Style.Apply(paragraph) paragraph = New NParagraph("Topic 1.1") section.Blocks.Add(paragraph) heading2Style.Apply(paragraph) AddParagraphs(section, "This is a sample paragraph from the first topic of chapter one.", 20) paragraph = New NParagraph("Topic 1.2") section.Blocks.Add(paragraph) heading2Style.Apply(paragraph) AddParagraphs(section, "This is a sample paragraph from the second topic of chapter one.", 20) ' Create chapter 2 paragraph = New NParagraph("Chapter 2") section.Blocks.Add(paragraph) heading1Style.Apply(paragraph) paragraph = New NParagraph("Topic 2.1") section.Blocks.Add(paragraph) heading2Style.Apply(paragraph) AddParagraphs(section, "This is a sample paragraph from the first topic of chapter two.", 20) paragraph = New NParagraph("Topic 2.2") section.Blocks.Add(paragraph) heading2Style.Apply(paragraph) AddParagraphs(section, "This is a sample paragraph from the second topic of chapter two.", 20) ' Update the table of contents m_RichText.Document.Evaluate() tableOfContents.Update() End Sub #End Region #Region "Implementation" Private Sub AddParagraphs(section As NSection, text As String, count As Integer) ' Duplicate the given text 5 times text = String.Join(" ", New String() {text, text, text, text, text}) ' Create the given number of paragraphs with the text For i = 0 To count - 1 section.Blocks.Add(New NParagraph(text)) Next End Sub #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_RichText As NRichTextView #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Table of Contents Example for WinForms Solution