Products Technologies Demo Docs Blog Support Company

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 different approaches will be outlined.

Two Ways to Restart Numbered Lists in TX Text Control

In order to restart a list, you can simply set the restart numbering property for the list item to true. However, this can lead to confusion, because when return is pressed, this property is inherited to the next item and it restarts again. This can be overcome in different ways. The first strategy is to use FormattingStyles. The second approach is to process this in code behind.

Using Formatting Styles to Format Lists

Let's say you want to start a document with several lists. For this you will create a style using the styles dialog. Create a new style, name it List and select the preferred Numbering in the format category. Make sure to choose Continue list for this style. Anytime you want to start a list, you can simply apply this style.

List

Now consider, you wish to separate the list in the middle. Then you will need a second list style. Create an new style, name it Restart and select the preferred numbering. Since you want to restart the list, choose Restart numbering this time. Also, and this is important, choose List as style for the following paragraph. This makes sure that the list continues when adding new list items. The style Restart can now be used any time a list should be restarted.

List

List

Restart Lists Using the TX Text Control API

If you prefer to implement this as part of your application to take this out of the hands of the end user, this approach is for you. A sample project can be found at the end of this article. The approach is simple: A global flag is used that indicates if the next inserted list should be continued. When the list at the current input position should be restarted, the flag is changed to true. The KeyUp event is used to check if the Enter key is pressed. If yes, the RestartNumbering property is changed to false if inside a list so that the numbering continues for the newly inserted list items.

// global flag that indicates whether the next inserted list item
// should be continued
private bool restartFlag = false;

// restarts the numbering
private void button1_Click(object sender, EventArgs e)
{
    textControl1.Selection.ListFormat.RestartNumbering = true;
    restartFlag = true;
}

// continues the following paragraph (item), if the global flag is true
private void textControl1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode != Keys.Return || restartFlag == false) return;

    if (textControl1.Selection.ListFormat.Type == TXTextControl.ListType.Numbered)
    {
        textControl1.Selection.ListFormat.RestartNumbering = false;
        restartFlag = false;
    }
}

Either way, restarting lists is very easy in Text Control and many ways lead to Rome. If you have further questions about these approaches, do not hesitate to contact us. Happy Coding!

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.FormattingStyle Class
  • TXTextControl.ListFormat.RestartNumbering Property

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

Requirements for this sample

  • Visual Studio 2019
  • TX Text Control .NET for Windows Forms X19

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 Post

Windows FormsWPF.NET

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.