Products Technologies Demo Docs Blog Support Company

Windows Forms and WPF: End a List on Return when Line is Empty

In TX Text Control, numbered and bulleted lists continue into each new paragraph by default. Handling the KeyPress event and detecting Enter on an empty line allows list formatting to be removed automatically. TextPartCollection ensures this logic applies across all document text parts.

Windows Forms and WPF: End a List on Return when Line is Empty

In the last blog entry, we introduced the TextPartCollection to modify content in all text parts of a document. This sample shows a typical scenario using this meta collection as this list type handling should work in all text parts including headers, footers and the main text.

When creating a numbered or bulleted list in TX Text Control, the following paragraph will inherit this formatting and the list will be continued in the next paragraph.

Windows Forms and WPF: End a list on return when line is empty

In some cases, you might want to end the list for example, if the last line is empty:

Windows Forms and WPF: End a list on return when line is empty

The following code is trapping the KeyPress event in order to check for the Enter key. In case, the last line is empty, the list format type is removed:

private void textControl1_KeyPress(object sender, KeyPressEventArgs e)
{
    switch (Convert.ToInt32(e.KeyChar))
    {
        // Enter key is pressed
        case 13:

            // get the currently active TextPart
            IFormattedText textPart =
              (IFormattedText)textControl1.TextParts.GetItem();

            // get the current line
            TXTextControl.Line line =
              textPart.Lines.GetItem(textPart.Selection.Start);

            // remove the numbered list format type, if the line is empty
            if (line.Text == "" || line.Text == "\r\n")
            {
                if (textPart.Selection.ListFormat.Type != 
                    TXTextControl.ListType.None)
                {
                    textPart.Selection.ListFormat.Type =
                        TXTextControl.ListType.None;
                }
            }

            break;
    }
}

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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 Posts

Windows FormsWPFTutorial

Using IFormattedText Objects to Access Elements Across All TextParts in a…

TX Text Control documents contain separate text areas including headers, footers, and text frames. TextPartCollection exposes them all as IFormattedText objects, enabling document-wide operations…


ASP.NETWindows FormsWPF

TX Text Control 34.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 34.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 34.0 SP1 is Now Available: What's New in the Latest Version

TX Text Control 34.0 Service Pack 1 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ActiveXASP.NETWindows Forms

Introducing TX Text Control 34.0: Your Next Leap in Document Processing

We are happy to announce the release of TX Text Control 34.0. This version is packed with new features and enhancements that will elevate your document processing experience. This version…


ASP.NETWindows FormsWPF

Sneak Peek: TX Text Control 34.0 Coming November 2025

We are excited to announce the upcoming release of TX Text Control 34.0, scheduled for November 2025. This update brings a host of new features and improvements to enhance your document processing…

Share on this blog post on: