Results 1 to 6 of 6

Thread: color coded script editor

  1. #1

    Thread Starter
    Lively Member astroanu2004's Avatar
    Join Date
    Jan 2008
    Location
    Sri Lanka
    Posts
    108

    Unhappy 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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: color coded script editor

    Welcome to the forums.

    What type of control are they typing in?

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  5. #5

    Thread Starter
    Lively Member astroanu2004's Avatar
    Join Date
    Jan 2008
    Location
    Sri Lanka
    Posts
    108

    Unhappy 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

    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?

  6. #6
    Lively Member
    Join Date
    Jul 2007
    Posts
    98

    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
  •  



Click Here to Expand Forum to Full Width