Introduction
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
Background
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...
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. )
IRTB2
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)
If irtb2.IsSyntaxEnabled Then
If Not words Is Nothing Then
Dim pos As Int32 = irtb2.SelectionStart
Dim oldc As Color = irtb2.SelectionColor
If useBlack Then
oldc = Drawing.Color.Black
End If
Dim cline As Integer = irtb2.GetLineFromCharIndex(irtb2.GetFirstCharIndexOfCurrentLine)
Dim start As Integer = irtb2.GetFirstCharIndexOfCurrentLine
IRTBTwo.LockWindowUpdate(irtb2.Handle)
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.DeselectAll()
irtb2.SelectionStart = pos
irtb2.SelectionColor = oldc
Next
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.DeselectAll()
irtb2.SelectionStart = pos
irtb2.SelectionColor = oldc
Next
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.DeselectAll()
irtb2.SelectionStart = pos
irtb2.SelectionColor = oldc
Next
i += i
Loop
End If
Next
IRTBTwo.LockWindowUpdate(IntPtr.Zero)
Dim E As New SyntaxHighlightingEventArgs(irtb2, _
SyntaxHighlightingEventArgs.SyntaxMethods.Regex)
irtb2.OnSyntaxApplied(E)
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
.)
Notes:
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.ClearHighlight(IrtbTwo1)
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))
ex:
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 GetLineCo
l()
. Use Line
and Collumn
properties, that returns those values.
Identing
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. |
FileNameInfo
|
Can store and retrieve filename. |
IsSyntaxEnabled
| Defines if Syntax highlighting methods will work |
LineNumberPictureBox
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.
IRTB2TabItem
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:
Global.IRTB.IRTB
Global.SuperApp.SuperApp
Never use Invalidate()
in excess: Your app will look bad.
Ctrl + Backspace = SuperQuick Backspace
History
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.
Previews:
2.0
2.1 'Precise Ness'
2.2 Regular expression
2.3 Oh Sorry
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.