color coded script editor
im creating a script editor with vb6 and having a problem im coloring special word. waht i want it to be is if the user is typing words such as function, dim , string etc(keywords) i want them to be in different colors just like in any IDE.
i dont want to use any API commands.:eek2:
Re: color coded script editor
Welcome to the forums. :wave:
What type of control are they typing in?
Re: color coded script editor
You will need to use a richtextbox if you want colors, and once you are using one you will probably need to use Instr() to see if the words exist and if they do, where the words are. Then you would use .SelStart, .SelLen and .SelColor to color them. Be warned however, IMO the richtextbox is a poorly written control and is difficult to use.
Re: color coded script editor
Here is a quick sample that uses one form and one module. The form requires a richtext box
Form Code
Code:
Option Explicit
Private Sub Form_Load()
InitializeWords
End Sub
Private Sub rtbEditor_Change()
ColorLastWord rtbEditor
End Sub
Module Code
Code:
Option Explicit
Private Type KeyWords
KeyWord As String
WordColor As Long
End Type
Dim tKeyWords(7) As KeyWords
Public Sub InitializeWords()
'Add keywords and make sure they are lowercase
tKeyWords(0).KeyWord = "option"
'Set the color for the keyword
tKeyWords(0).WordColor = vbBlue
tKeyWords(1).KeyWord = "explicit"
tKeyWords(1).WordColor = vbBlue
tKeyWords(2).KeyWord = "private"
tKeyWords(2).WordColor = vbBlue
tKeyWords(3).KeyWord = "sub"
tKeyWords(3).WordColor = vbBlue
tKeyWords(4).KeyWord = "end"
tKeyWords(4).WordColor = vbRed
tKeyWords(5).KeyWord = "byval"
tKeyWords(5).WordColor = vbBlue
tKeyWords(6).KeyWord = "as"
tKeyWords(6).WordColor = vbBlue
tKeyWords(7).KeyWord = "string"
tKeyWords(7).WordColor = vbBlue
End Sub
Public Sub ColorLastWord(txtBox As RichTextBox)
Dim strText As String
Dim strWords() As String
Dim strLastWord As String
Dim lngLast As Long
'Store the cursor position
lngLast = txtBox.SelStart
'Work with the text box as a space delimited string
strText = txtBox.Text
strText = Replace(strText, vbCrLf, " ")
strText = Replace(strText, vbTab, " ")
'Remove extra spaces
Do While InStr(strText, " ") > 0
strText = Replace(strText, " ", " ")
Loop
strWords = Split(strText, " ")
'Find the last word in the text box
strLastWord = strWords(UBound(strWords))
ColorWord txtBox, strLastWord, lngLast
End Sub
Private Sub ColorWord(txtBox As RichTextBox, ByVal Word As String, ByVal EndPos As Long)
Dim strWord As String
Dim i As Integer
Dim ub As Integer
Dim blnColor As Boolean
Dim lngStart As Long
ub = UBound(tKeyWords)
strWord = Trim(LCase(Word))
'Find if the word has a special character before or after it
If InStr(strWord, "(") > 0 Then
'If there is an open paren before word color from that point on
strWord = Mid(strWord, InStr(strWord, "(") + 1)
End If
lngStart = EndPos - Len(strWord)
Select Case Right(strWord, 1)
'if there is a closing paren or comma color up to it
Case ")", ","
strWord = Left(strWord, Len(strWord) - 1)
End Select
'Loop through the key words to see if we have a match
For i = 0 To ub
If strWord = tKeyWords(i).KeyWord Then
blnColor = True
Exit For
End If
Next i
'Color the word
With txtBox
.SelStart = lngStart
.SelLength = Len(strWord)
If blnColor Then
.SelColor = tKeyWords(i).WordColor
Else
.SelColor = vbBlack
End If
.SelStart = EndPos
.SelColor = vbBlack
End With
End Sub
color coded script editor
the code was great but the problem is if i start typing from a middle of a word the colors go wrong :eek:
and the other problem is ; i have a file and i'm loading it with the rihtext control's load file method, but how can i put color changes to that text?
And i dont get the keyword "UBound" what is that word used for? :wave:
Re: color coded script editor
you have to override the loadfile method. Since you want a specific presentation of the data you must open the file as text, read it preprocess it and load the preprocessed data to an algorithm that applies the colors for the key words