You know how when you type in VB, key word and
comments etc. are in different colours, who
would you write a code for this?
Any help is appreciated.
Printable View
You know how when you type in VB, key word and
comments etc. are in different colours, who
would you write a code for this?
Any help is appreciated.
Do you mean how do you change the colours for the keywords, comments etc in VB? Or are you wanting to write an editor that will display keywords etc in colours?
If the former is the case then go to Tools, Options, then the Editor Format tab. You can change the colours there.
If the latter is the case then i can't offer too much help other than you'd have to use a richtextbox so you can change the colours and then just parse the text as you enter it into the box, checking for keyowrds, comments etc <you'd need a list of keywords, and have to define how a comment is made> and changing the colour of a particular part of the text as you go. Hmm, actually i read something about this (changing colurs in a text box) on an article in either this site or vbsquare.
If i find it i'll post another reply.
Well, this is probably not the best solution, but I just got my a$$ kicked in QuakeIII, so I'm not in the greatest mood for writing code :)
Just start a project, add a RichTextbox (Go to Project\Components... and check "Microsoft Rich Textbox Control 6.0"), and paste the following code into the form's code window. Add whatever keywords you like (I got it started with a few).
Hopefully it's readable, 'cause I 'aint got time for comments (school in the morning :( )...
That's it! It actually took more time to figure this out than I'd like to admit, but it was a fun challenge.Code:Option Explicit
Private Sub HighlightText(sKeyword As String)
Dim nStart As Integer, _
sPrevChar As String, _
sNextChar As String
nStart = InStr(1, LCase(RichTextBox1.Text), sKeyword)
Do While nStart <> 0
If nStart > 1 Then
sPrevChar = Mid$(RichTextBox1.Text, nStart - 1, 1)
Else
sPrevChar = " "
End If
If Len(RichTextBox1.Text) >= nStart + Len(sKeyword) Then
sNextChar = Mid$(RichTextBox1.Text, nStart + Len(sKeyword), 1)
Else
sNextChar = " "
End If
If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
sPrevChar = Chr(10) Or sPrevChar = Chr(9)) And _
(sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
sNextChar = Chr(10) Or sNextChar = Chr(9)) Then
With RichTextBox1
.SelStart = nStart - 1
.SelLength = Len(sKeyword)
.SelColor = vbBlue
.SelStart = Len(RichTextBox1.Text)
.SelColor = vbBlack
End With
End If
nStart = InStr(nStart + Len(sKeyword), LCase(RichTextBox1.Text), sKeyword)
Loop
End Sub
Private Sub RichTextBox1_Change()
With RichTextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SelColor = vbBlack
.SelStart = Len(.Text)
End With
HighlightText "if"
HighlightText "else"
HighlightText "elseif"
HighlightText "then"
HighlightText "for"
HighlightText "each"
HighlightText "next"
HighlightText "do"
HighlightText "while"
HighlightText "loop"
HighlightText "select"
HighlightText "case"
HighlightText "end"
HighlightText "with"
HighlightText "or"
HighlightText "and"
HighlightText "dim"
HighlightText "private"
HighlightText "public"
HighlightText "function"
HighlightText "sub"
HighlightText "property"
HighlightText "integer"
HighlightText "long"
HighlightText "single"
HighlightText "double"
HighlightText "string"
HighlightText "boolean"
End Sub
Hope that helps,
~seaweed