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 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 TXText
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:
WPF
In WPF, the approach is slightly different and another method has to be used. The TXText
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.
Also See
This post references the following in the documentation:
TX Text Control .NET for Windows Forms
- TXText
Control. Text Control Class - TXText
Control. Text Control. Document To Client Method
TX Text Control .NET for WPF
- TXText
Control. WPF. Text Control Class - TXText
Control. WPF. Text Control. Point To Document Method
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
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.
Related Posts
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…
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…
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…
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.
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…