Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C# 4.0

Line Numbers for RichText Control in C#

Rate me:
Please Sign up or sign in to vote.
4.89/5 (22 votes)
11 Aug 2009CDDL3 min read 158.9K   7.4K   64   39
Display line numbers for rich text box with auto sizing. This is a converted version from another article written in VB.NET.

Introduction

Please wait while more details are being updated.

This article is a converted project from VB.NET to C#, originally created by nogChoco in the article, "LineNumbers for the RichTextBox".

Sure anyone can make a line numbering user control.  I've made many variations of them for VB 3.0 to 6.0 & C++, always using picture boxes or whatever as a drawing base to mark the position. When Microsoft released the newer versions of RichText control boxes (in more recent versions), the old methods went out the door.  By this, I mean that one can easily zoom in & out of the box by holding down Ctrl (or Shift) key while using the mouse wheel, posing a HUGE problem for traditional methods. 

Q: Why use RichText instead of the traditional TextBox?
A: (In older versions) RichText can hold more data! (and offers more features)

LineNumbers.png

Before Compiling Fore-note  

I have successfully compiled & ran this example project under both Visual Studio 2008 & SharpDevelop 3.x using Framework 2.0.

You will probably have problems running this example project under VS 2005 & lower; do note the control does work under the Framework 2.0If you are having problems running this under VS 2005 or other studios, you might want to either find yourself a project converter or upgrade your studio. I am very sorry if this is an inconvenience for you, that is why this is posted for Intermediate & Expert users. 

If you want a free IDE that can run this, try out SharpDevelop IDE.

Compiling Your Own Project

LineNumbers_-_ex3.png

Using SharpDevelop (3.x) 

  1. Create a new C# Windows project
  2. Add Reference to your project (Right click 'References' > 'Add Reference')
  3. Under GAC Tab select: "Microsoft.VisualBasic"
  4. Insert into your project existing item.. "linenum_rtf.cs"
  5. Under Custom Components, insert "LineNumbers_For_RichTextBox" into your form as well as insert a RichTextBox
  6. Line them up side by side (left side by default)
  7. Link Line Numbers to your RichTextBox
    1. Under "Add LineNumbers to" > "Parent Rich Text Box":
    2. Select your RichTextBox to add line numbers to
    3. (feel free to customize it on your own)
  8. Run the project!

Using Visual Studio (2008)

  1. Create a new C# Windows Project
  2. Add Reference to your project (Right click 'References' > 'Add Reference')
  3. Under .NET Tab select: "Microsoft.VisualBasic"
  4. Insert the existing item into your project... "linenum_rtf.cs"
  5. Build your project!
    1. This will not show under Custom Components unless you build you the project first!
  6. Under Custom Components, insert "LineNumbers_For_RichTextBox" into your form as well as insert a RichTextBox
  7. Line them up side by side (left side by default)
  8. Link Line Numbers to your RichTextBox
    1. Under "Add LineNumbers to" > "Parent Rich Text Box":
    2. Select your RichTextBox to add line numbers to
    3. (feel free to customize it on your own)
  9. Run the project!

Background and Points of Interest

As a software designer/developer like all of you, time is of the essence when meeting deadlines! This example using the RichText control works great! Especially with the zoom in/out feature using the mouse wheel, and it added nice graphical features which can be easily customized. However, I feel the urging need to program in C# and this example was only provided in VB.NET... It was time to upgrade it & now, on with the show!

History

  • 2009.08.05 - Initial conversion from VB.NET to C#
  • 2009.08.08 - Updated notes on this page (pictures soon to come)

To Do

  • Provide 2 versions
    • Graphical: The current version (customizable and beautiful)
    • Simple: Side bar is simple & has low overhead on your projects
  • Strip out references to Microsoft.VisualBasic so it's more pure C#

- Damian J. Suess of Xeno Innovations, Inc. [2000-2009]

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
CEO Xeno Innovations, Inc.
United States United States
[ Exceeding the Limits of Imagination ]

Comments and Discussions

 
BugSmall memory leak Pin
Lyr9-Jun-20 3:41
Lyr9-Jun-20 3:41 
QuestionGetting Line numbers from charindex? Pin
JasonJBurden15-Oct-19 6:18
JasonJBurden15-Oct-19 6:18 
QuestionAdd line numbers dinamically Pin
Member 121352084-Jan-17 22:12
Member 121352084-Jan-17 22:12 
AnswerRe: Add line numbers dinamically Pin
Damian Suess24-Feb-17 9:18
Damian Suess24-Feb-17 9:18 
GeneralRe: Add line numbers dinamically Pin
Member 1213520824-Feb-17 9:50
Member 1213520824-Feb-17 9:50 
QuestionQuestion regarding showing first linenumber when starting app... Pin
Member 1204891814-Nov-15 16:32
Member 1204891814-Nov-15 16:32 
GeneralПревосходно! :) Pin
Ksardas6-Aug-15 9:59
Ksardas6-Aug-15 9:59 
GeneralRe: Превосходно! :) Pin
Damian Suess13-Nov-15 3:33
Damian Suess13-Nov-15 3:33 
QuestionWonderful Project Pin
Member 1108237715-Feb-15 7:57
Member 1108237715-Feb-15 7:57 
AnswerRe: Wonderful Project Pin
Damian Suess16-Mar-15 14:44
Damian Suess16-Mar-15 14:44 
QuestionGood Pin
Elegiac19-May-13 21:45
Elegiac19-May-13 21:45 
AnswerRe: Good Pin
Damian Suess3-Jun-13 11:17
Damian Suess3-Jun-13 11:17 
QuestionVisual Studio 11 support Pin
verber0017-Jun-12 17:11
verber0017-Jun-12 17:11 
AnswerRe: Visual Studio 11 support Pin
Damian Suess3-Jun-13 11:14
Damian Suess3-Jun-13 11:14 
GeneralMy vote of 5 Pin
Panki_17-Oct-11 20:36
Panki_17-Oct-11 20:36 
GeneralRe: My vote of 5 Pin
Damian Suess13-Feb-15 13:37
Damian Suess13-Feb-15 13:37 
Generalstatic 'Main' method not suitable for an entry point Pin
501st_commander26-Apr-10 10:10
501st_commander26-Apr-10 10:10 
QuestionLicense??? Pin
Tannner.R9-Apr-10 13:18
Tannner.R9-Apr-10 13:18 
AnswerRe: License??? Pin
Damian Suess10-Apr-10 7:02
Damian Suess10-Apr-10 7:02 
GeneralRe: License??? Pin
Tannner.R10-Apr-10 7:10
Tannner.R10-Apr-10 7:10 
GeneralRe: License??? Pin
Tannner.R5-May-10 8:41
Tannner.R5-May-10 8:41 
GeneralOne more thing - if you want to solicit input and maintain changes... Pin
Cheeso31-Mar-10 13:25
Cheeso31-Mar-10 13:25 
Questionline numbers disappear when the # of lines is large? Pin
Cheeso31-Mar-10 13:23
Cheeso31-Mar-10 13:23 
I noticed that the line numbers seemed to disappear when I displayed an RTB with hundreds of lines. ??

I looked in Update_SizeAndPosition() and found that the edge of the linenumbers control was being moved outwards, either left or right, depending on how it was docked to the parent RTB. When there were, say, 4 digits to display, and the linenumbers control was displayed on the left, the left edge of it would move further leftward. This became a problem when the control was anchored to the left edge of the form.

So I reworked the Update_SizeAndPosition() method, based on the principle that the bounding box of the linenumebers control plus its parent should not change, regardless of how wide the digits display could become. In other words, when docked to the left of the RTB, the left edge of the linenumbers control should never move further leftward, and the right edge of the RTB should never move further rightward.

Conversely, when docked to the right edge of the RTB, the right edge of the linenumbers control should nevevr move further rightward, and the left edge of the RTB should never move further leftward. If that makes sense.

The only movement, then, should be on the internal borderline, between the two controls. As the linenumbers control needs to expand, the RTB should shrink, horizontally.

The modified code looks like this:

else if (zDockSide != LineNumberDockSide.None)
{
    // --- DockSide is active L/R/H

    // line up the tops of the numbers and the RTB
    zNewLocation.Y = zParent.Top;
    zNewSize.Height = zParent.Height;

    // Principle: keep the overall size of the RTB and
    // the RtbLineNumbers, the same.
    // --------------------------------------------
    // The left edge of the RtbLineNumbers must remain
    // fixed when it is displayed to the left.  The
    // right edge of the RtbLineNumbers when it is
    // displayed to the right.
    // --------------------------------------------
    //
    // Corollary: only move the border between the two.

    if (zAutoSizing_Size.Width > 0)
        zNewSize.Width = zAutoSizing_Size.Width;

    int delta = zNewSize.Width - this.Size.Width;

    if (delta == 0) return; // no change!


    var newParentSize = zParent.Size;
    newParentSize.Width = newParentSize.Width - delta;

    if (zDockSide == LineNumberDockSide.Left)
    {
        // The line numbers appear to the left of the RTB.

        // For the left edge to remain fixed,
        // this.Location.X  must remain constant.
        // Move the left border of the RTB and resize it,
        // as necessary.

        var newParentLoc = zParent.Location;
        newParentLoc.X = zNewLocation.X + zNewSize.Width + 1;
        zParent.Location = newParentLoc;
    }
    else if (zDockSide == LineNumberDockSide.Right)
    {
        // The line numbers appear to the right of the RTB.

        // For the right edge to remain fixed,
        // this.Location.X + this.Width must remain constant
        // Move the right border of the RTB as necessary (hence,
        // resize it).
        zNewLocation.X = this.Location.X - delta;
    }
    zParent.Size = newParentSize;
    this.Location = zNewLocation;
    this.Size = zNewSize;
}

AnswerRe: line numbers disappear when the # of lines is large? Pin
Damian Suess10-Apr-10 7:05
Damian Suess10-Apr-10 7:05 
GeneralRe: line numbers disappear when the # of lines is large? Pin
Cheeso20-Apr-10 4:14
Cheeso20-Apr-10 4:14 

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

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