Click here to Skip to main content
Click here to Skip to main content

Numbering lines of RichTextBox in .NET 2.0

, 3 Nov 2005
Rate this:
Please Sign up or sign in to vote.
The standard RichTextBox does not allow numbering of lines. This user control does.


Numbering of lines in text editors is a well known feature. But the standard RichTextBox in .NET 2.0 does not support this feature. It is also hard to find a suitable solution on the Internet. Especially, a solution that does not directly use Win32 functions.

The RichTextBox is not a standard control in Windows Forms. It does not use the OnPaint method and other similar functions as it should and it also hides some of the important properties required for proper customization. One way to overcome this is to use win32 API functions and override the WndProc functions. I don't like this way of doing it but I am forced to use it again and again. I consider it as a fault of .NET developers.

Fortunately, for numbering lines of RichTextBox, there is a satisfactory solution that does not use pure Win32 API functions.


We implement the RichTextBox with numbered lines as a UserControl. We will not override anything in the RichTextBox, we will only use its events. Our UserControl named NumberedTextBoxUC consists of SplitContainer, Label (numberLabel) and RichTextBox. Label is used for displaying the line number and RichTextBox for the text content, both are contained in SplitterContainer.

The content of numberLabel is updated in the RichTextBox's event handlers. These events are:

  • OnTextChanged
  • OnVScroll
  • OnSizeChanged
  • OnFontChanged


There are several problems with this implementation. The first one is scrolling. Unlike the VS source code editor or TextBox control, RichTextBox uses smooth scrolling, thus scrolling with the scrollbar scrolls the text in pixels, not in lines. You will notice that the first line is displayed in half. This is not always a wanted behaviour and I would appreciate the possibility to turn it off. Another problem is the redrawing speed of large Labels, you cannot afford to print too many lines to Label in each OnTextChanged event handler. Another strange problem is the RichTextBox in .NET 2.0 uses strange line indentation, which is impossible to turn off or set to zero. The same font in Label and RichTextBox results in different line positions when the controls are top aligned.


I am displaying only the numbers of visible lines, thus the unnecessary hidden line numbers are not printed. The update function is called updateNumberLabel(). It uses the RichTextBox functions GetCharIndexFromPosition and GetLineFromCharIndex to determine the first and last visible line numbers.

private void updateNumberLabel()
    //we get index of first visible char and 
    //number of first visible line
    Point pos = new Point(0, 0);
    int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
    int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);

    //now we get index of last visible char 
    //and number of last visible line
    pos.X = ClientRectangle.Width;
    pos.Y = ClientRectangle.Height;
    int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
    int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);

    //this is point position of last visible char, we'll 
    //use its Y value for calculating numberLabel size
    pos = richTextBox1.GetPositionFromCharIndex(lastIndex);

    //finally, renumber label
    numberLabel.Text = "";
    for (int i = firstLine; i <= lastLine + 1; i++)
        numberLabel.Text += i + 1 + "\n";


For different line indentations I have found a constant, which works best for font size 8. The size of the Label font is bigger for this constant. I hope, in future versions of .NET this issue will be fixed and both the fonts will be exactly the same, as it was in .NET 1.1.

public NumberedTextBoxUC()

    numberLabel.Font = new Font(richTextBox1.Font.FontFamily, 
                              richTextBox1.Font.Size + 1.019f);

Smooth scrolling is another issue which causes a lot of troubles. I use the small numberLabel location update in each OnVScroll event handler. The Label is moved about as many pixels different from multiples of the RichTextBox font height, thus the modulo text position of the font height. The reverse solution, to update the text position to be line aligned, is in my opinion impossible without using Win32 functions. Updating the text position with RichTextBox functions in this way results in text shivering.

private void richTextBox1_VScroll(object sender, EventArgs e)
    //move location of numberLabel for amount 
    //of pixels caused by scrollbar
    int d = richTextBox1.GetPositionFromCharIndex(0).Y % 
                              (richTextBox1.Font.Height + 1);
    numberLabel.Location = new Point(0, d);



I hope this user control helps developers handling RichTextBox. I appreciate your advice and improvements to this control.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Petr Minarik
Web Developer
Czech Republic Czech Republic
Petr Minarik is currently studying at Czech Technical University Prague. He is interested in C# programming, Direct3D and graphics generally and he develops his own realtime 3D scene editor. He likes fun, beer and good people.

You can visit him at his homepage.

Comments and Discussions

BugBUG Pinmembergao sir26-Nov-12 1:55 
GeneralRe: BUG PinmemberDamian J. Suess13-Feb-15 14:38 
GeneralMy vote of 1 Pinmemberxxbbcc19-Nov-12 18:48 
Very fragile implementation and extremely specific to the .NET version, font size, scroll and zoom settings.
GeneralMy vote of 5 PinmemberFarhan Ghumra23-Aug-12 3:10 
QuestionSlight improvement PinmemberJoS@Work12-Jun-12 1:16 
GeneralRe: Slight improvement Pinmemberbenyoman17-Aug-12 14:19 
QuestionAnother one control to try Pinmemberantgraf30-Oct-11 10:17 
GeneralMy vote of 1 PinmemberMember 451749722-Jul-10 3:58 
GeneralUncode support Pinmemberaldycool7-Apr-10 1:16 
GeneralWidth auto-adjustment and better management of first line offset and last existing line in RichTextBox PinmemberMember 15499213-Jul-09 10:56 
QuestionRe: Width auto-adjustment and better management of first line offset and last existing line in RichTextBox Pinmemberzarawebfx8-Jan-10 4:11 
QuestionHow about indent Pinmemberdfpcnc17-Jun-09 11:55 
GeneralDelete numbering lines of RichTextBox in c# PinmemberSAADRAFID14-May-09 2:21 
GeneralProblems with Scroll PinmemberDiegoCol18-Jun-08 7:45 
QuestionSome problems PinmemberChenQiang8-Dec-07 5:17 
GeneralRe: Some problems PinmemberRodUbi11-Dec-07 8:31 
GeneralNumbers only painted as far as original height Pinmemberjimmygyuma31-Oct-07 5:08 
GeneralRe: Numbers only painted as far as original height Pinmemberjimmygyuma31-Oct-07 6:00 
GeneralAnother font size fix Pinmemberrareshh21-Jun-07 0:04 
GeneralGood Job PinmemberMike Hankey23-Apr-07 13:42 
GeneralDisable smooth scrolling Pinmemberfirefoxxi22-Apr-07 5:28 
GeneralDisable smooth wheelscrolling Pinmembermlr7929-Dec-07 6:08 
GeneralLine number Pinmemberpline12-Feb-07 1:29 
QuestionCustom horizontal scrollbar Pinmembercyberjunky3-Dec-06 16:42 
GeneralwndProc Pinmembercurlypaul3-Nov-06 5:21 
GeneralDon't Use a Label Pinmemberwaynebe21-Oct-06 10:55 
GeneralRe: Don't Use a Label Pinmemberalphons8-Jul-07 6:01 
GeneralRe: Don't Use a Label PinmemberM-Dayyan7-May-08 2:18 
GeneralRe: Don't Use a Label Pinmemberalphons7-May-08 5:55 
GeneralRe: Don't Use a Label PinmemberM-Dayyan7-May-08 16:51 
QuestionRe: Don't Use a Label Pinmemberd581g5-Jul-09 4:49 
AnswerRe: Don't Use a Label PinmemberAlphons van der Heijden7-Jul-09 0:37 
GeneralAdding the Control to a project... PinmemberMattthe cooldude9-Oct-06 21:23 
Generalgood work. I was looking for something like this only Pinmembervik2011-Sep-06 23:09 
QuestionAny idea how to add a comboboxes next to the line numbers? PinmemberCristoff14-May-06 10:31 
Generalthank you Pinmembersskset13-Apr-06 16:19 
GeneralSaving the text PinmemberTheNewNewGuy26-Mar-06 23:57 
GeneralFix to font size problem Pinmemberjoelycat8-Feb-06 10:47 
AnswerRe: Fix to font size problem Pinmembermondesser12-Nov-07 23:42 
GeneralProblem resizing your control Pinmemberctorob22-Dec-05 2:09 
GeneralRe: Problem resizing your control Pinmembern0n4m34-Apr-06 8:09 
GeneralRe: Problem resizing your control Pinmemberbevans19752-Jul-07 9:43 
GeneralRe: Problem resizing your control Pinmemberbevans19752-Jul-07 10:20 
General'wordwrap enabled' problem PinmemberAngryC25-Nov-05 21:29 
GeneralRe: 'wordwrap enabled' problem PinmemberAngryC28-May-06 20:24 
Generaltip for font problem Pinmembertaffkoi20-Nov-05 0:00 
GeneralRe: tip for font problem PinmemberPetr Minarik20-Nov-05 1:38 
QuestionHow about .NET 1.x, and also close/expand like in Visual Studio PinmemberJeff X. Chang3-Nov-05 9:03 
AnswerRe: How about .NET 1.x, and also close/expand like in Visual Studio PinmemberPetr Minarik3-Nov-05 9:19 
GeneralImage missing Pinmembermalharone3-Nov-05 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 3 Nov 2005
Article Copyright 2005 by Petr Minarik
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid