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 TX Text Control .NET for Windows Forms
TXTextControl Namespace
TextControl Class
The TextControl class implements a Windows Forms control with high-level text editing features.
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 TX Text Control .NET for Windows Forms
TXTextControl Namespace
TextControl Class
DocumentToClient Method
Computes coordinates in twips relative to the top-left corner of the document into client coordinates.
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);
}
}
view raw test.cs hosted with ❤ by GitHub

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 TX Text Control .NET for WPF
WPF Namespace
TextControl Class
PointToDocument Method
Converts a Point that represents the current coordinate system of the TextControl into a point in document coordinates, which are in twips relative to the top-left corner of the whole document.
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 TX Text Control .NET for WPF
WPF Namespace
TextControl Class
The WPF.TextControl class class implements a control with high-level text editing features.
.

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.