How to show number of a line in a RichTextBox C#

I tried re-using the code from the codeproject articles referenced elsewhere, but every option I looked at, seemed a bit too kludgy.

So I built another RichTextBoxEx that displays line numbers.

The line numbering can be turned on or off. It's fast. It scrolls cleanly. You can select the color of the numbers, the background colors for a gradient, the border thickness, the font, whether to use leading zeros. You can choose to number lines "as displayed" or according to the hard newlines in the RTB.

Examples:

enter image description here

enter image description here

enter image description here

It has limitations: it displays numbers only on the left. You could change that without too much effort if you cared.

The code is designed as C# project. Though it's part of a larger "solution" (an XPath Visualization tool), the custom RichTextBox is packaged as a separable assembly, and is ready to use in your new projects. In Visual Studio, just add a reference to the DLL, and you can drag-and-drop it onto your design surface. You can just discard the other code from the larger solution.

See the code