Click here to Skip to main content
15,881,173 members
Articles / Desktop Programming / Windows Forms

Fast Colored TextBox for Syntax Highlighting

Rate me:
Please Sign up or sign in to vote.
4.97/5 (877 votes)
24 Oct 2014LGPL323 min read 7.1M   104.2K   1.3K  
Custom text editor with syntax highlighting
Imports FastColoredTextBoxNS
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms

Namespace TesterVB
    Friend Class GifImageStyle
        Inherits TextStyle

        Private parent As FastColoredTextBox

        Private timer As Timer

        Public Property ImagesByText() As Dictionary(Of String, Image)

        Public Sub New(parent As FastColoredTextBox)
            MyBase.New(Nothing, Nothing, FontStyle.Regular)
            Me.ImagesByText = New Dictionary(Of String, Image)()
            Me.parent = parent
            Me.timer = New Timer()
            Me.timer.Interval = 100
            AddHandler Me.timer.Tick, Sub()
                                          ImageAnimator.UpdateFrames()
                                          parent.Invalidate()
                                      End Sub
            Me.timer.Start()
        End Sub

        Public Sub StartAnimation()
            For Each image As Image In Me.ImagesByText.Values
                If ImageAnimator.CanAnimate(image) Then
                    ImageAnimator.Animate(image, New EventHandler(AddressOf Me.OnFrameChanged))
                End If
            Next
        End Sub

        Private Sub OnFrameChanged(sender As Object, args As EventArgs)
        End Sub

        Public Overrides Sub Draw(gr As Graphics, position As Point, range As Range)
            Dim text As String = range.Text
            Dim iChar As Integer = range.Start.iChar
            While text <> ""
                Dim replaced As Boolean = False
                For Each pair As KeyValuePair(Of String, Image) In Me.ImagesByText
                    If text.StartsWith(pair.Key) Then
                        Dim i As Single = CSng(pair.Key.Length * range.tb.CharWidth) / CSng(pair.Value.Width)
                        If i > 1.0F Then
                            i = 1.0F
                        End If
                        text = text.Substring(pair.Key.Length)
                        Dim rect As RectangleF = New RectangleF(CSng(position.X + range.tb.CharWidth * pair.Key.Length / 2) - CSng(pair.Value.Width) * i / 2.0F, CSng(position.Y), CSng(pair.Value.Width) * i, CSng(pair.Value.Height) * i)
                        gr.DrawImage(pair.Value, rect)
                        position.Offset(range.tb.CharWidth * pair.Key.Length, 0)
                        replaced = True
                        iChar += pair.Key.Length
                        Exit For
                    End If
                Next
                If Not replaced AndAlso text.Length > 0 Then
                    Dim r As Range = New Range(range.tb, iChar, range.Start.iLine, iChar + 1, range.Start.iLine)
                    MyBase.Draw(gr, position, r)
                    position.Offset(range.tb.CharWidth, 0)
                    text = text.Substring(1)
                End If
            End While
        End Sub
    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Freelancer
Ukraine Ukraine
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.

Comments and Discussions