Click here to Skip to main content
15,437,709 members
Articles / Programming Languages / Visual Basic
Posted 4 Jan 2013


12 bookmarked


Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
11 Sep 2013LGPL35 min read
This is an alternative for "Improved RichTextBox - IRTB". 2.3 is out! Finnaly with some Regex Fixing and Alpha of Alphas in identing!

Download Source and Dlls 


This is an edited version of Juan's IRTB. It is a class inheriting RichTextBox with many codes  I found around the web.  

The  major edit is support to highlight. In this release,I fixed problems with syntax highlighting and added default syntax definition for Python and Batch 


Why IRTB2 is useful? 

  • Because its features can help anyone working even in Tabbed Text Editors.
  • It's easy to use (syntax highlighting can be a bit hard. Even when I were testing, I had trouble with my own creation :P)
  • Binds two ideas of dream of syntax highlight, and good Text editor.  

What is different from IRTB? 

  • Changed property making useless use of TextChanged events to set a var to true or false  
  • LineNumberPictureBox instead of a PB in a UserControl
  • FileNameInfo Property to store and retrieve filenames.
  • Syntax highlighting subs (Captain Obvious know.) . They're shared, so... 
  • VB.NET
    RegexHighlinghting.HighlightKeyWordRegex(CType(TabControl1.SelectedTab.Controls.Item(0), IRTBTwo), c, cz(0))
            RegexHighlighting.HighlightKeyWordRegex(CType(TabControl1.SelectedTab.Controls.Item(0), IRTBTwo), c2, cz(1)) 

    They're maybe bad. 

  • Have no missing original RTB props\subs 

Using the controls. 

The features of IRTB2:  

Syntax Highlight (now it's mine. I fixed a bug highlighting words in middle of others by creating a entire new sub.)  

Line Col (by Juan)  

Line Numbering (Juan too. I created a option turning possible highlight current line in a different Brush. ) 


Syntax Highlight 

I've fixed problem with the Regex Highlight. Shrunk and improved sub!  

Public Shared Sub HighlightKeyWordRegex(ByVal irtb2 As IRTBTwo, _
        ByVal words As List(Of String), ByVal color As Color, Optional ByVal useBlack As Boolean = True, _
        Optional ByVal HighlightRange As HighlightRange = HighlightRange.All)
    'Is syntax enabled?
    If irtb2.IsSyntaxEnabled Then
        'LIST IS NULL?
        If Not words Is Nothing Then
            'store pos
            Dim pos As Int32 = irtb2.SelectionStart
            'store old color\DFC
            Dim oldc As Color = irtb2.SelectionColor
            If useBlack Then
                oldc = Drawing.Color.Black
            End If
            'store line to prevent problem with range c. line.
            Dim cline As Integer = irtb2.GetLineFromCharIndex(irtb2.GetFirstCharIndexOfCurrentLine)
            Dim start As Integer = irtb2.GetFirstCharIndexOfCurrentLine
            'Before anything, lock to no *evil* flick

            For Each word As String In words
                If HighlightRange = HighlightRange.All Then
                    Dim x As MatchCollection = New Regex(word.ToUpper).Matches(irtb2.Text.ToUpper)
                    For Each Match As Match In x

                        irtb2.Find(word, Match.Index, RichTextBoxFinds.WholeWord)
                        irtb2.SelectionColor = color
                        irtb2.SelectionStart = pos
                        irtb2.SelectionColor = oldc
                ElseIf HighlightRange = HighlightRange.CurrentLine Then
                    Dim x As MatchCollection = New Regex(word.ToUpper).Matches(irtb2.Lines(cline).ToUpper)
                    For Each Match As Match In x
                        irtb2.Find(word, start + Match.Index, RichTextBoxFinds.WholeWord)
                        irtb2.SelectionColor = color
                        irtb2.SelectionStart = pos
                        irtb2.SelectionColor = oldc
                ElseIf HighlightRange = HighlightRange.View Then
                    Dim i As Integer = irtb2.GetFirstVisibleLine
                    Dim x As MatchCollection
                    Do While i < irtb2.GetLastVisibleLine
                        x = New Regex(word.ToUpper).Matches(irtb2.Lines(i).ToUpper)
                        For Each Match As Match In x
                            irtb2.Find(word, irtb2.GetFirstCharIndexFromLine(i) + _
                                    Match.Index, RichTextBoxFinds.WholeWord)
                            irtb2.SelectionColor = color
                            irtb2.SelectionStart = pos
                            irtb2.SelectionColor = oldc
                        i += i
                End If
            'Free to evil, flick!
            Dim E As New SyntaxHighlightingEventArgs(irtb2, _
        End If
    End If
End Sub

In this way, you will provide a IRTB2, a list with the words, and a color.

This routine, the position and old color are stored and a Win32 API call care about the flick (I've seen it with my own eyes). Then it will find each match as whole word, coloring it. Then, the old position and color restore. And then flick will be back.  

Anyways, let's move to a quick tutorial. 

It's simple. (IRTB2 will be referred as IrtbTwo1.)


List shouldn't be null. It will bug the control.  To prevent it, I added a null check in the code. 

You'll need to call HighlightKeyWordRegex (I misspelled s) multiple times if you want two or more colors of highlight. 

Dim c As New List(Of String)
    Dim c2 As New List(Of String)
    Dim cz As Color() = {Color.Blue, Color.Purple}
    Private Sub ds()
        On Error GoTo ex
           IRTBTwo.HighlightComments(CType(TabControl1.SelectedTab.Controls.Item(0), IRTBTwo), c, cz(0))
           RegexHighlighting.HighlightKeyWordRegex(CType(TabControl1.SelectedTab.Controls.Item(0), IRTBTwo), c, cz(0))
           RegexHighlighting.HighlightKeyWordRegex(CType(TabControl1.SelectedTab.Controls.Item(0), IRTBTwo), c2, cz(1))
    Exit Sub 

First, we'll clear any colored text, including imprecise syntax highlighting. 

We got two lists (their content are defined other place). There's a Color Array, with one color for each list.

Then we call it two times and we're ready to double colors! 

Get Line Col  

This is the line 0 col 0 fixed code:

Dim GetFirstCharIndex As Integer = Me.GetFirstCharIndexOfCurrentLine
Dim GetRTBLine = Me.GetLineFromCharIndex(GetFirstCharIndex)
Dim GetPosition As Integer = Me.SelectionStart - GetFirstCharIndex
GetPosition = GetPosition + 1
GetRTBLine = GetRTBLine + 1
Dim x As Integer() = {GetRTBLine, GetPosition}
Return x   

We Get Index of current line's first char to use in both ops. The line is calculated by getting a line from the charindex. To get col, we subtract the Selection's start position with charindex.

It would result in a value minus one the real one, so end up adding one more to the resulting values. 

To get it, you don't need to call GetLineCol(). Use Line and Collumn properties, that returns those values.


Auto identing is not yet supported. Thanks to the guy who created the internet so I can go out there and release it on 2.4 or 2.5. 

It only adds (length) spaces to the selections, 'cause it errors. Just call CreateIdent or RemoveIdent.  

New Properties  in 2.3 

Property   Desc 
Line  Gets current line 
Column  Get current column 
DefaulCharOffset  Four Predefined Values to CharOffset.  


Can store and retrieve filename.  


Defines if Syntax highlighting methods will work 


The basic utility of this is draw line numbers. 

You can set a BG, too.   

Well, since it usually throws a NullRefEx when used, let I explain how to use it:

How to use correctly: 

Add a L.N.P.B. to your form. It will usually throw a NullRefEx. Set MyIRTB to the wanted IRTB2. If NullRefEx still appears, close and open the designer again. Call Ready() when needed to draw the line numbers. 


Useless. It's just a TabPage with a IRTB2 inside. You still need to use CType

Points of Interest   

I've discovered a rule: Never do IRTB.IRTB, SuperApp.SuperApp. Each time you compile, you'll need to change:


Never use Invalidate() in excess: Your app will look bad.  

Ctrl + Backspace = SuperQuick Backspace 


End of World - v1.0 

3 Jan 2013 - 2.0

  • Fixed Regex Highlight.
  • Added Changed, FileNameInfo Props|LNPB ,IRTB2TI controls

 7 Jan 2013 - 2.1 'The Precise Ness' 

  • -Support to comments (beta). Can be set with CommentPrefix(e.g. ' or //) and CommentColor (e.g Green) and calling HighlightComments().  
  • -More Precise syntax highlight with ClearHighlight() method.  This is done by blacking all text and calling the highlight methods. 

12 Jan 2013 - 2.2 'Regular Expression 

  • Supports comments fully ok(<KnownIssue>HTML don't work.</KnownIssue>
  • Added default defs(DefaultDefs class) 
  • Added RegexHighlighting class. HighlightKeyWordRegex was moved here. There's also HighlightPattern class, for highlighting the good 'n' old Regular expression patterns.
  • HighlightPattern's values can be only set in constructor. 

10 Sep 2013 - 2.3 'Oh, sorry! 

  • Python and Java learning latered my work.
  • Added Default definitions for Python and Batch.
  • Find() native method in RichTextBox was replaced by true Regex due to bugs. Now it uses regex with "\b & wordinlist & \b"
  • Some methods which are shared uselessly were made instance only. It was very annoying even for me. Affected HighlightComments and ClearHighlight
  •  Identing "pre alpha-alpha" was added.
  • Fixed current line highlight range(view, oh man. I still can't resolve it).
  • ClearHighlight() method can now be associated with a HighlightRange, so now you can use Range CurrentLine without losing syntaxed previous lines. 



Image 1 

2.1 'Precise Ness'  

Image 2

2.2 Regular expression  

 Image 3

2.3 Oh Sorry

 Image 4

Note for downloaders:  

 Due to the fact I restarted the project due to Circular dependency bug, there are two DLLs, 123.dll and IRTB2.dll. 

Unlike obviously, choose IRTB2.dll. That's the right DLL.   

Coming Soon:

Newbie's Guide. 


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Written By
Brazil Brazil
Hi guys! I live in Piauí,Brazil. My friends say that I'm nerd, miracle, because I started programming at 8 years old.
I know VB.NET and a bit of C♯.
I started programming at 2009 when the most current .NET Framework was v3.5 with VB Express 2008.
At 2011, I updated to .NET 4.0(VBx and CSx) and 2012 I tried SharpDevelop, however, I only use it to convert C♯ projects to VB.NET; few times I use to create projects.

Comments and Discussions

QuestionImplementation Pin
euoqryuoqpwg24-Jan-13 21:56
Membereuoqryuoqpwg24-Jan-13 21:56 
GeneralRe: Implementation Pin
Globin28-Jan-13 7:38
MemberGlobin28-Jan-13 7:38 
GeneralRe: Implementation Pin
skyscanner31-Mar-17 22:34
Memberskyscanner31-Mar-17 22:34 
NewsA Good and a Bad news Pin
Globin17-Jan-13 2:56
MemberGlobin17-Jan-13 2:56 
Question5+ Pin
Brisingr Aerowing4-Jan-13 14:20
professionalBrisingr Aerowing4-Jan-13 14:20 
GeneralRe: 5+ Pin
Globin5-Jan-13 5:31
MemberGlobin5-Jan-13 5:31 
GeneralRe: 5+ Pin
Brisingr Aerowing5-Jan-13 8:17
professionalBrisingr Aerowing5-Jan-13 8:17 
GeneralGood work and thanks! Pin
Juan R. Huertas4-Jan-13 9:17
MemberJuan R. Huertas4-Jan-13 9:17 
GeneralRe: Good work and thanks! Pin
Globin5-Jan-13 3:45
MemberGlobin5-Jan-13 3:45 

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.