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

Line Numbers for RichText Control in C#

, 11 Aug 2009 CDDL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Damian J. Suess
CEO Xeno Innovations, Inc.
United States United States
[ Exceeding the Limits of Imagination ]

Comments and Discussions

 
QuestionGood PinmemberElegiac19-May-13 21:45 
AnswerRe: Good PinmemberDamian J. Suess3-Jun-13 11:17 
QuestionVisual Studio 11 support Pinmemberverber0017-Jun-12 17:11 
AnswerRe: Visual Studio 11 support PinmemberDamian J. Suess3-Jun-13 11:14 
GeneralMy vote of 5 PinmemberPanki_17-Oct-11 20:36 
Generalstatic 'Main' method not suitable for an entry point Pinmember501st_commander26-Apr-10 10:10 
QuestionLicense??? PinmemberTannner.R9-Apr-10 13:18 
AnswerRe: License??? PinmemberDamian J. Suess10-Apr-10 7:02 
GeneralRe: License??? PinmemberTannner.R10-Apr-10 7:10 
GeneralRe: License??? PinmemberTannner.R5-May-10 8:41 
GeneralOne more thing - if you want to solicit input and maintain changes... PinmemberCheeso31-Mar-10 13:25 
Questionline numbers disappear when the # of lines is large? PinmemberCheeso31-Mar-10 13:23 
AnswerRe: line numbers disappear when the # of lines is large? PinmemberDamian J. Suess10-Apr-10 7:05 
GeneralRe: line numbers disappear when the # of lines is large? PinmemberCheeso20-Apr-10 4:14 
Damian, after looking at this further, I thought to try a different tack.
Rather than defining a companion control, as you did here, I extended RichTextBox. This RichTextBoxEx displays line numbers itself, within its bounds, in response to the Paint event. Some pictures.
 
http://i39.tinypic.com/13zcoz6.jpg[^]
 
http://i43.tinypic.com/wml2z9.jpg[^]
 
http://i39.tinypic.com/25i4x3o.jpg[^]
 
It scrolls cleanly, it's fast.
 
You can get the code to see what I did, here:
http://xpathvisualizer.codeplex.com/SourceControl/changeset/view/42057#823009[^]
 
Also this one is under source control, so if anyone wants to submit requests or bugs, it's available to you.
BugRe: line numbers disappear when the # of lines is large? PinmemberHirnhamster16-Jan-12 2:10 
QuestionI cant find the control PinmemberAlexanderBorup27-Feb-10 13:50 
AnswerRe: I cant find the control PinmemberDamian J. Suess28-Feb-10 8:24 
Generalsome mod's to make it a bit more c#: PinmemberRob24121-Jan-10 21:38 
GeneralRe: some mod's to make it a bit more c#: PinmemberDamian J. Suess2-Jan-10 5:22 
QuestionWhen it is a lot of lines Pinmemberrave4ka5-Oct-09 7:31 
AnswerRe: When it is a lot of lines PinmemberDamian J. Suess2-Jan-10 5:21 
GeneralRe: When it is a lot of lines PinmemberCheeso31-Mar-10 13:16 
QuestionRe: When it is a lot of lines PinmemberPanki_17-Oct-11 20:27 
Generalvery good. PinmemberZielonekk4-Sep-09 0:01 
GeneralGet the line number when clicking on the control Pinmemberkenneth_cwc22-Aug-09 5:54 
GeneralMy vote of 5 Pinmemberarvindjo11-Aug-09 23:33 
GeneralJust on time... PinmemberPaul Selormey10-Aug-09 14:34 

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 | Mobile
Web04 | 2.8.141015.1 | Last Updated 11 Aug 2009
Article Copyright 2009 by Damian J. Suess
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid