Results 1 to 4 of 4

Thread: Change text colors

  1. #1

    Thread Starter
    Addicted Member rbnwares's Avatar
    Join Date
    Sep 2000
    Location
    Philippines
    Posts
    142

    Wink

    Is there any short way of changing a text contained in the rich textbox? I do want to create an editor that behaves like VB IDE and other great editors. For example, I want to change the color of the comment text to green, ordinary text to blue and keywords to blue.
    Stupidity is better than cure.

    VB6 SP5 Enterprise Ed.
    C, Pascal, VC++ 6.0


    Running Win98 SE and Win2000 Prof. Ed.


    Email me at : [email protected]

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    no.
    here's how i did it for existing text:
    set the text selection boundaries to include the text to modify.
    then do a richtextbox.selcolor=newcolor
    and unset the selection (or put it back to original)

    For new text being parsed in, simply change the Richtextbox.selcolor as you go.

    By the way, i found a bug in the richtextbox control.
    when you have just the cursor on screen, and no text
    selected and try to read the data of the text next to the cursor, it returns the values for the letter to the LEFT of the cursor, not the right. Quite annoying. I had a custom toolbar that updated pressed buttons depending on the value of the character next to the cursor (italic button, bold button, etc), and this really messed up how my program worked.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  3. #3
    Guest
    Add to a Form with a RichTextBox.
    Code:
    Option Explicit
    Const vbDarkBlue = &H800000
    Const vbDarkGreen = &H8000&
    
    
    Private Sub HighlightText(sKeyword As String, iColour As Long)
        
        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 = vbDarkBlue
                    .SelText = StrConv(.SelText, vbProperCase)
                    .SelStart = Len(RichTextBox1.Text)
                    .SelColor = iColour
                End With
            End If
                                                      
            nStart = InStr(nStart + Len(sKeyword), LCase(RichTextBox1.Text), sKeyword)
        Loop
    
    End Sub
    
    Private Sub Form_Resize()
        RichTextBox1.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub
    
    Private Sub RichTextBox1_Change()
    With RichTextBox1
        .SelStart = 0
        .SelLength = Len(.Text)
        .SelColor = vbBlack
        .SelStart = Len(.Text)
    End With
    
                                        
    ' Add more custom words here
    HighlightText "if", vbBlue
    HighlightText "then", vbBlue
    HighlightText "end", vbBlue
    HighlightText "else", vbBlue
    HighlightText "open", vbBlue
    End Sub

  4. #4

    Thread Starter
    Addicted Member rbnwares's Avatar
    Join Date
    Sep 2000
    Location
    Philippines
    Posts
    142

    Cool

    Wow! Thanks a lot for ur help. I will try it as soon as possible.
    Stupidity is better than cure.

    VB6 SP5 Enterprise Ed.
    C, Pascal, VC++ 6.0


    Running Win98 SE and Win2000 Prof. Ed.


    Email me at : [email protected]

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