Products Technologies Demo Docs Blog Support Company

Displaying Line Numbers in Windows Forms and WPF

Displaying line numbers on a page has been an often requested feature and this sample shows how to calculate the required positions.

Displaying Line Numbers in Windows Forms and WPF

Displaying line numbers on a page has been an often requested feature. These numbers are not part of the document content. A typical application for this feature is medical transcription where line numbering is required. This can be implemented using TX Text Control with little effort.

Windows Forms

In case of Windows Forms, the line numbers are drawn on top of TXTextControl.TextControl class using a graphics drawing object. The challenge here is to calculate the position of the line object in client coordinates. In TX Text Control X15, a new method has been introduced to simplify this calculation. The TXTextControl.TextControl.DocumentToClient method converts the coordinates in twips in relation to the top-left corner of the document to client coordinates.

private void setLineNumbering()
{
    textControl1.Refresh();

    Graphics g = textControl1.CreateGraphics();

    // iterates all lines
    foreach (TXTextControl.Line line in textControl1.Lines)
    {
        // left position of current line minus offset
        // so that the line number is displayed next to the current line
        int left = line.TextBounds.X - LINE_START_OFFSET;
        //top position of the current line
        int top = line.TextBounds.Top;
        //converts the positon of the current line to client coordinates
        Point p = textControl1.DocumentToClient(new Point(left, top));

        PointF pos = new PointF(p.X , p.Y);

        //draws line number to the specified position
        g.DrawString(line.Number.ToString() + ":",
            new Font(textControl1.Font.Name, textControl1.Font.Size),
            LINE_COLOR, pos);
    }
}

The following screenshot shows the rendered line numbers in the left margin area:

Line numbering in Windows Forms

WPF

In WPF, the approach is slightly different and another method has to be used. The TXTextControl.WPF.TextControl.PointToDocument method has also been introduced in version X15, which converts a document point in the current TextControl coordinate system to client coordinates. This method is used to calculate the correct position of text blocks containing line numbers. The text blocks are then added to a canvas control that is displayed on top of TXTextControl.WPF.TextControl class.

The DocumentToClient() and PointFromDocument() methods are helpful methods and have various applications when client coordinates have to be calculated.

Download the sample from our GitHub repository.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

TX Text Control .NET for Windows Forms

  • TXTextControl.TextControl Class
  • TXTextControl.TextControl.DocumentToClient Method

TX Text Control .NET for WPF

  • TXTextControl.WPF.TextControl Class
  • TXTextControl.WPF.TextControl.PointToDocument Method

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 2017 or better
  • TX Text Control .NET for Windows Forms (trial sufficient)

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

ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 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 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.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

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…


ActiveXASP.NETWindows Forms

Expert Implementation Services for Legacy System Modernization

We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.


ActiveXASP.NETWindows Forms

Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5

TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…