Click here to Skip to main content
15,904,652 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralUsing XP theme in VB6 Pin
Andy H23-Apr-04 5:09
Andy H23-Apr-04 5:09 
GeneralArray.IndexOf with an array of structures Pin
ebbie23-Apr-04 3:31
ebbie23-Apr-04 3:31 
GeneralRe: Array.IndexOf with an array of structures Pin
Colin Angus Mackay23-Apr-04 8:27
Colin Angus Mackay23-Apr-04 8:27 
GeneralColor Coding Text Pin
mtone23-Apr-04 3:12
mtone23-Apr-04 3:12 
GeneralRe: Color Coding Text Pin
PaleyX23-Apr-04 3:24
PaleyX23-Apr-04 3:24 
GeneralRe: Color Coding Text Pin
mtone23-Apr-04 6:38
mtone23-Apr-04 6:38 
GeneralRe: Color Coding Text Pin
PaleyX24-Apr-04 5:30
PaleyX24-Apr-04 5:30 
GeneralRe: Color Coding Text (Long Entry) Pin
Aaron Eldreth24-Apr-04 17:33
Aaron Eldreth24-Apr-04 17:33 
Howdy mtone,

I am working on a control that will be a VS.Net equavelent that will cover syntax coloring. It will be free and I'll post it on CodeProject when I'm finished, but I'm not to close to finishing right now (school isn't helping a lot either).

There are several ways you could do it with the existing RichTextbox.

If you aren't worrying about speed, I would recommend Matt's article[^]. He covers the issue, but it's really, really, really slow.

If speed is an important factor, you might want to go with the SelectionStart(), SelectionLength(), and SelectionColor() properties.

This is some code that I made back in January before I started working on my own non-inherited RichEdit control. It colors JavaScript syntax, but can easily be modified to your needs. Note: I did not use RegEx because I wanted to keep it as "light as possible"

VB
<br />
<br />
Public JSWords() As KeyWords<br />
<br />
Structure KeyWords<br />
Dim Word As String<br />
Dim Color As Color<br />
End Structure<br />
<br />
ReDim JSWords(8)<br />
JSWords(0).Word = "if"<br />
JSWords(0).Color = Color.Blue<br />
JSWords(1).Word = "then"<br />
JSWords(1).Color = Color.Blue<br />
JSWords(2).Word = "else"<br />
JSWords(2).Color = Color.Blue<br />
JSWords(3).Word = "for"<br />
JSWords(3).Color = Color.Blue<br />
JSWords(4).Word = "while"<br />
JSWords(4).Color = Color.Blue<br />
JSWords(5).Word = "not"<br />
JSWords(5).Color = Color.Blue<br />
JSWords(6).Word = "function"<br />
JSWords(6).Color = Color.Blue<br />
JSWords(7).Word = "return"<br />
JSWords(7).Color = Color.Blue<br />
JSWords(8).Word = "var"<br />
JSWords(8).Color = Color.Blue<br />
<br />
Public Sub JSColorVisibleLines(ByVal rtb As RichTextbox)<br />
        Dim FirstLine As Integer = FirstVisibleLine()<br />
        Dim LastLine As Integer = LastVisibleLine()<br />
        Dim FirstVisibleChar As Integer<br />
        Dim i As Integer = FristLine<br />
<br />
        If (FirstLine = 0) And (LastLine = 1) Then<br />
            Exit Sub<br />
        Else<br />
            While i < LastLine<br />
<br />
                FirstVisibleChar = rtb.CharFromLineIndex(FirstLine)<br />
<br />
                JSColorLineNumber(rtb, FirstLine, FirstVisibleChar)<br />
<br />
                FirstLine += 1<br />
                i += 1<br />
            End While<br />
        End If<br />
    End Sub<br />
<br />
    Public Sub JSColorLineNumber(ByVal rtb As RichTexbox, ByVal LineIndex As Integer, ByVal lStart As Integer)<br />
        Dim Line As String = rtb.Lines(LineIndex)<br />
        Dim i As Integer = 0<br />
        Dim Instance As Integer<br />
<br />
        ' Lock the update<br />
        LockUpdate()<br />
<br />
        rtb.SelectionStart = lStart<br />
        rtb.SelectionLength = Line.Length<br />
        rtb.SelectionColor = Color.Black<br />
<br />
        Instance = InStr(Line, "//")<br />
        If Instance <> 0 Then<br />
            rtb.SelectionStart = (lStart + Instance - 1)<br />
            rtb.SelectionLength = (Line.Length - Instance + 1)<br />
            rtb.SelectionColor = Color.Green<br />
        End If<br />
<br />
        If Instance = 1 Then<br />
            ' Unlock the update and exit<br />
            UnLockUpdate()<br />
            Exit Sub<br />
        End If<br />
<br />
        While i < JSWords.Length<br />
<br />
            Instance = InStr(Line, JSWords(i).Word)<br />
<br />
            If Instance <> 0 Then<br />
                rtb.SelectionStart = (lStart + Instance - 1)<br />
                rtb.SelectionLength = JSWords(i).Word.Length<br />
                rtb.SelectionColor = JSWords(i).Color<br />
            End If<br />
<br />
            i += 1<br />
        End While<br />
<br />
        ' Unlock the update<br />
        rtb.UnLockUpdate()<br />
    End Sub<br />
<br />
Public Function FirstVisibleLine() As Integer<br />
<br />
            'Return rtb.GetLineFromCharIndex(rtb.GetCharIndexFromPosition(New Point(50, rtb.Font.Height / 2)))<br />
            Return SendMessage(rtb.Handle.ToInt32, EditMessages.GetFirstVisibleLine, 0, 0)<br />
        End Function<br />
<br />
Public Function LastVisibleLine() As Integer<br />
<br />
            Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)<br />
<br />
            If LastLine > Me.LineCount Or LastLine = 0 Then<br />
                LastLine = Me.LineCount<br />
            End If<br />
            Return LastLine<br />
        End Function<br />
<br />
Public Sub LockUpdate()<br />
            LockWindowUpdate(Handle.ToInt32)<br />
        End Sub<br />
<br />
Public Sub UnLockUpdate()<br />
            LockWindowUpdate(0)<br />
        End Sub<br />


Good Luck

Aaron Eldreth
TheCollective4.com
GeneralRe: Color Coding Text Pin
Aaron Eldreth24-Apr-04 17:37
Aaron Eldreth24-Apr-04 17:37 
GeneralRe: Color Coding Text Pin
mtone26-Apr-04 7:23
mtone26-Apr-04 7:23 
GeneralRe: Color Coding Text Pin
Aaron Eldreth26-Apr-04 15:28
Aaron Eldreth26-Apr-04 15:28 
GeneralRe: Color Coding Text Pin
mtone27-Apr-04 4:19
mtone27-Apr-04 4:19 
GeneralRe: Color Coding Text Pin
Aaron Eldreth27-Apr-04 16:36
Aaron Eldreth27-Apr-04 16:36 
QuestionFormatting text as the user types - how? Pin
N.T.Gopalakrishnan23-Apr-04 2:42
N.T.Gopalakrishnan23-Apr-04 2:42 
AnswerRe: Formatting text as the user types - how? Pin
Aaron Eldreth24-Apr-04 17:07
Aaron Eldreth24-Apr-04 17:07 
GeneralRe: Formatting text as the user types - how? Pin
N.T.Gopalakrishnan25-Apr-04 17:59
N.T.Gopalakrishnan25-Apr-04 17:59 
GeneralRe: Formatting text as the user types - how? Pin
Aaron Eldreth27-Apr-04 16:47
Aaron Eldreth27-Apr-04 16:47 
QuestionHow to make an Event Calendar in Windows Application by using vb.net ? Pin
rG8223-Apr-04 1:54
rG8223-Apr-04 1:54 
GeneralRandom Access Files Pin
Pickerd23-Apr-04 0:08
Pickerd23-Apr-04 0:08 
Generalwhy o why.. Pin
Anonymous22-Apr-04 23:38
Anonymous22-Apr-04 23:38 
GeneralRe: why o why.. Pin
Dave Kreskowiak23-Apr-04 0:00
mveDave Kreskowiak23-Apr-04 0:00 
GeneralDumb question... Pin
Halonix22-Apr-04 16:21
Halonix22-Apr-04 16:21 
GeneralRe: Dumb question... Pin
Mike Ellison22-Apr-04 17:37
Mike Ellison22-Apr-04 17:37 
GeneralUsing the property browser Pin
PaleyX22-Apr-04 11:59
PaleyX22-Apr-04 11:59 
GeneralClosing a form during create handle Pin
maf66622-Apr-04 9:53
maf66622-Apr-04 9:53 

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.