|
-
Jan 18th, 2008, 08:13 AM
#1
Thread Starter
Lively Member
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.
-
Jan 18th, 2008, 08:18 AM
#2
Re: color coded script editor
Welcome to the forums. 
What type of control are they typing in?
-
Jan 18th, 2008, 12:07 PM
#3
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.
-
Jan 18th, 2008, 03:03 PM
#4
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
-
Jan 23rd, 2008, 05:07 AM
#5
Thread Starter
Lively Member
-
Jan 23rd, 2008, 05:47 AM
#6
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|