Click here to Skip to main content
15,559,667 members
Articles / Programming Languages / C#
Posted 8 Jan 2008


161 bookmarked

Advanced Text Editor with Ruler

Rate me:
Please Sign up or sign in to vote.
4.85/5 (46 votes)
20 Mar 2008CPOL3 min read
Extending RichTextBox with ruler and much more


About two years ago I was creating a project for university. It was a complex of five programs that were related to testing (checking knowledge I mean). One of them contained a text editor. But unfortunately, I didn't know how to do that, that's why it was very simple. So, after these two years, I decided to rewrite my program in C# .NET (initially, it was written in VB.NET) and create a new powerful text editor. You can see part of it here.


I spent a lot of time(!) searching the Internet for controls like this, but the best ones are shareware and others did not fit my needs. So, what does this control have? I tried to make it look like Microsoft Word and I think that there are some similarities. The ruler lets you change the following: left and right margins, left indent, hanging indent and right indent. You also can disable margins (their values are set to 1). You can see how it looks in the picture above.

Also, you can add tabs by clicking on the control with the left mouse button. But it is allowed only inside the area bounded by margins. If you want to remove a tab, just drag it off from the control.

The editor lets you use lists, underline styles, advanced char styles (you can create your own links, that are not words starting with "http://" or even "www"), OLE functionality is also available. I want to thank Oscar Londoño for his article Inserting Images into a RichTextBox Control (The OLE Way). It helped me a lot to deal with OLE. This project contains his code.

Using the Code

You can use the code according to the CPOL.

Projects are created as Windows Applications, but you can easily convert them into *.dll or just embed code into your project.

Unfortunately, I removed Visual Studio 2005, so, sorry but I can't create and upload a Visual Studio 2005 project. But you can import all required files into a Visual Studio 2005 project without any problems. However, note that Visual Studio 2008 added some new namespaces (like LINQ) that Visual Studio 2005 does not "understand". Just remove them. That's all. You are ready.

Points of Interest

One thing that shocked me is that Microsoft has released RichTextBox 6.0! (It is distributed with Microsoft Office 2007) but... with one exception. There is no documentation about its features. All that I've found is a list of added functions. You can find it here. There are also descriptions for all released versions of RichTextBox.


  • 7 January 2008: Initial release
  • 16 January 2008: Projects for Visual Studio 2005 and 2008 posted instead of standalone control.
  • 26 January 2008: Posted completed AdvancedTextEditor project. It is available for Visual Studio 2005 and Visual Studio 2008.
  • 10 February 2008: Fixed PrintDialog bug. It didn't receive focus when shown. Now it's OK. Thanks to Chris Schucker for bug report and recommendations.
  • 22 February 2008: Added new functionality (full text justification, underline styles and colors and other)
  • 24 February 2008: Fixed bug with conversion from millimeter to pixel and vice versa Thanks to Chris Schucker. Also corrected bug which caused incorrect display of the indents.
  • 19 March 2008: Version 2.0 released. A lot of changes including lists, OLE, underline styles, etc.


I understand that I am not able to include everything into the control, that's why you can freely add something useful or change code to fit your needs. But, please inform me about that, just email me at It's just to let anyone use an upgraded (and corrected) version of that control. Of course, advisors will be mentioned.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Kazakstan Kazakstan
Currently I am studying at East Kazakhstan State Technical University. My future occupation is engineer-programmer.

I use Visual Basic, C#, Delphi and a little C++. Also I am interested in using SQL and Perl.

Comments and Discussions

QuestionUtility of "old style formating" region ? Pin
moimael6-Sep-08 2:28
moimael6-Sep-08 2:28 
AnswerRe: Utility of "old style formating" region ? Pin
Aleksei Karimov10-Sep-08 6:27
Aleksei Karimov10-Sep-08 6:27 
GeneralRe: Utility of "old style formating" region ? Pin
minhvc21-Sep-08 17:44
minhvc21-Sep-08 17:44 
Questiontranslation to vb2005 Pin
frankelman28-Jul-08 12:08
frankelman28-Jul-08 12:08 
AnswerRe: translation to vb2005 Pin
Aleksei Karimov16-Aug-08 21:45
Aleksei Karimov16-Aug-08 21:45 
GeneralRe: translation to vb2005 Pin
frankelman23-Aug-08 6:01
frankelman23-Aug-08 6:01 
GeneralRe: translation to vb2005 [modified] Pin
nyt197215-Jul-11 0:38
professionalnyt197215-Jul-11 0:38 
GeneralRe: translation to vb2005 Pin
nyt197218-Jul-11 22:48
professionalnyt197218-Jul-11 22:48 
This my Forms Designer code.

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Editor
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Dim CharStyle1 As ExtendedRichTextBox.CharStyle = New ExtendedRichTextBox.CharStyle
        Dim ParaLineSpacing1 As ExtendedRichTextBox.ParaLineSpacing = New ExtendedRichTextBox.ParaLineSpacing
        Dim ParaListStyle1 As ExtendedRichTextBox.ParaListStyle = New ExtendedRichTextBox.ParaListStyle
        Me.ExtendedRichTextBox1 = New ExtendedRichTextBox
        Me.MenuStrip1 = New System.Windows.Forms.MenuStrip
        Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
        Me.ToolStrip2 = New System.Windows.Forms.ToolStrip
        Me.StatusStrip1 = New System.Windows.Forms.StatusStrip
        Me.ExtendedRichTextBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ExtendedRichTextBox1.Location = New System.Drawing.Point(0, 77)
        Me.ExtendedRichTextBox1.Name = "ExtendedRichTextBox1"
        Me.ExtendedRichTextBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes
        CharStyle1.Bold = False
        CharStyle1.Italic = False
        CharStyle1.Link = False
        CharStyle1.Strikeout = False
        CharStyle1.Underline = False
        Me.ExtendedRichTextBox1.SelectionCharStyle = CharStyle1
        Me.ExtendedRichTextBox1.SelectionFont2 = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Inch)
        ParaLineSpacing1.ExactSpacing = 0
        ParaLineSpacing1.SpacingStyle = ExtendedRichTextBox.ParaLineSpacing.LineSpacingStyle.Unknown
        Me.ExtendedRichTextBox1.SelectionLineSpacing = ParaLineSpacing1
        ParaListStyle1.BulletCharCode = CType(0, Short)
        ParaListStyle1.NumberingStart = CType(0, Short)
        ParaListStyle1.Style = ExtendedRichTextBox.ParaListStyle.ListStyle.NumberAndParenthesis
        ParaListStyle1.Type = ExtendedRichTextBox.ParaListStyle.ListType.None
        Me.ExtendedRichTextBox1.SelectionListType = ParaListStyle1
        Me.ExtendedRichTextBox1.SelectionOffsetType = ExtendedRichTextBox.OffsetType.None
        Me.ExtendedRichTextBox1.SelectionSpaceAfter = 0
        Me.ExtendedRichTextBox1.SelectionSpaceBefore = 0
        Me.ExtendedRichTextBox1.Size = New System.Drawing.Size(608, 360)
        Me.ExtendedRichTextBox1.TabIndex = 0
        Me.ExtendedRichTextBox1.Text = ""
        Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
        Me.MenuStrip1.Name = "MenuStrip1"
        Me.MenuStrip1.Size = New System.Drawing.Size(608, 24)
        Me.MenuStrip1.TabIndex = 1
        Me.MenuStrip1.Text = "MenuStrip1"
        Me.ToolStrip1.Location = New System.Drawing.Point(0, 24)
        Me.ToolStrip1.Name = "ToolStrip1"
        Me.ToolStrip1.Size = New System.Drawing.Size(608, 25)
        Me.ToolStrip1.TabIndex = 2
        Me.ToolStrip1.Text = "ToolStrip1"
        Me.ToolStrip2.Location = New System.Drawing.Point(0, 49)
        Me.ToolStrip2.Name = "ToolStrip2"
        Me.ToolStrip2.Size = New System.Drawing.Size(608, 25)
        Me.ToolStrip2.TabIndex = 3
        Me.ToolStrip2.Text = "ToolStrip2"
        Me.StatusStrip1.Location = New System.Drawing.Point(0, 440)
        Me.StatusStrip1.Name = "StatusStrip1"
        Me.StatusStrip1.Size = New System.Drawing.Size(608, 22)
        Me.StatusStrip1.TabIndex = 4
        Me.StatusStrip1.Text = "StatusStrip1"
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(608, 462)
        Me.MainMenuStrip = Me.MenuStrip1
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub
    Friend WithEvents ExtendedRichTextBox1 As ExtendedRichTextBox
    Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
    Friend WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
    Friend WithEvents ToolStrip2 As System.Windows.Forms.ToolStrip
    Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip

End Class

Please Please Please help. The Warnings it show in VB 2005 are

The variable 'CharStyle1' is either undeclared or was never assigned.
The variable 'ParaLineSpacing1' is either undeclared or was never assigned.
The variable 'ParaListStyle1' is either undeclared or was never assigned.

The Code runs ok, but when I want to goto design mode then it shows thses warning. Please help me as soon as possible.
QuestionVisual Basic 2008 Express Edition Pin
mr_fj24-Jul-08 11:46
mr_fj24-Jul-08 11:46 
AnswerRe: Visual Basic 2008 Express Edition Pin
Aleksei Karimov25-Jul-08 3:21
Aleksei Karimov25-Jul-08 3:21 
AnswerRe: Visual Basic 2008 Express Edition Pin
mr_fj26-Jul-08 15:22
mr_fj26-Jul-08 15:22 
GeneralRe: Visual Basic 2008 Express Edition Pin
DIMO1329-Aug-08 0:19
DIMO1329-Aug-08 0:19 
GeneralZoomfactor Pin
diamondhh6-Jun-08 5:33
diamondhh6-Jun-08 5:33 
GeneralRe: Zoomfactor Pin
Aleksei Karimov8-Jun-08 23:24
Aleksei Karimov8-Jun-08 23:24 
GeneralAbout pagebreak Pin
Win32nipuh28-May-08 21:27
professionalWin32nipuh28-May-08 21:27 
GeneralRe: About pagebreak Pin
Aleksei Karimov28-May-08 23:34
Aleksei Karimov28-May-08 23:34 
Questiongreat, what about VS2005? Pin
Win32nipuh22-May-08 2:38
professionalWin32nipuh22-May-08 2:38 
AnswerRe: great, what about VS2005? Pin
Aleksei Karimov22-May-08 9:44
Aleksei Karimov22-May-08 9:44 
GeneralBug Pin
nazimahmed31-Mar-08 3:12
nazimahmed31-Mar-08 3:12 
GeneralRe: Bug Pin
Aleksei Karimov31-Mar-08 8:28
Aleksei Karimov31-Mar-08 8:28 
GeneralWPF Version Pin
Len202025-Mar-08 16:57
Len202025-Mar-08 16:57 
GeneralRe: WPF Version Pin
Aleksei Karimov26-Mar-08 0:18
Aleksei Karimov26-Mar-08 0:18 
QuestionFont Size error Pin
minhvc20-Mar-08 22:59
minhvc20-Mar-08 22:59 
GeneralRe: Font Size error [modified] Pin
Aleksei Karimov21-Mar-08 2:12
Aleksei Karimov21-Mar-08 2:12 
QuestionSomething in new version Pin
minhvc20-Mar-08 22:55
minhvc20-Mar-08 22:55 

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.