Results 1 to 10 of 10

Thread: Text Colouring

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    If i have a rich text box that has text in it how can i make it run through each charachter at a time and colour it if it is a "<" when a button is clicked ????

  2. #2
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    Try this
    Code:
    Public Sub colorize()
        Dim posEnd, posStart, x, i As Integer
        
        x = 0
        i = 0
    
        For i = 0 To Len(RichTextBox1.Text)
            RichTextBox1.SelStart = i
            RichTextBox1.SelLength = 1
    
            If RichTextBox1.SelText = ">" Then  'start tag
                posStart = i
                RichTextBox1.SelColor = vbRed
            
            End If
    
        Next i
    
    End Sub
    Have Fun
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Thankyou very much, nice code !!!!!!!!!!!!!!!!!

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Um, what if i want to colour all of the text but to different colours, i know how to do this except it misses some, is there anyway to slow the code down like a 10 millisecond pause before it does the next charachter ??

  5. #5
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'adding to the code supplied by PRIVATE1
    '
    Option Explicit
    
    Public Sub Colorize()
        
        Dim posEnd As Integer, posStart As Integer
        Dim x As Integer, i As Integer, j As Integer
        Dim myArr(5)
        
        myArr(0) = vbRed
        myArr(1) = vbBlue
        myArr(2) = vbYellow
        myArr(3) = vbMagenta
        myArr(4) = vbGreen
        myArr(5) = vbCyan
        
        RichTextBox1.BackColor = vbBlack
        
        x = 0   'start
        i = 0   'start
       Randomize
       
        For i = 0 To Len(RichTextBox1.Text)
            RichTextBox1.SelStart = i
            RichTextBox1.SelLength = 1
    
            posStart = i
            j = Int(Rnd * 5) + 1
            RichTextBox1.SelColor = myArr(j)
            
        Next i
        
        End Sub
        
    Private Sub cmdQuit_Click()
        Unload Me
    End Sub
    
    Private Sub Command1_Click()
       Call Colorize
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    What i want is like for the '=' to be coloured blue and the '"' to be coloured green except that when i do this because the for loop runs so quick it misses half the charachters, can i slow the loop down at the end just before the next so that it has to do that before it colours the next ??

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I don't have a problem with it missing any, the only
    problem is it can't distinguish between " and "" or """"
    it just takes all versions of " and colors them green.

    Code:
    For i = 0 To Len(RichTextBox1.Text)
            RichTextBox1.SelStart = i
            RichTextBox1.SelLength = 1
    
            If RichTextBox1.SelText = "=" Then  'start tag
                posStart = i
                RichTextBox1.SelColor = vbBlue
            End If
            If RichTextBox1.SelText = """" Then
               posStart = i
               RichTextBox1.SelColor = vbGreen
            End If
        Next i
    ps.Running a 650 k7 128Ram so speed is not a problem.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Your problem is that sellength = 1 so its only finding " whereas u need a seperate function where sellength = 2 and another where it is = 3 to findout i also needs to be incremented by 2 or 3 as well

  10. #10
    Guest
    Add the following to a Form with a RichTextBox
    Code:
    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
                                                               
    
                With RichTextBox1
                    .SelStart = nStart - 1
                    .SelLength = Len(sKeyword)
                    .SelColor = iColour
                    .SelText = UCase(sKeyword)
                    .SelStart = Len(RichTextBox1.Text)
                    .SelColor = iColour
                End With
                                                      
            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 ">", vbBlue
        HighlightText "<", vbBlue
        HighlightText """", vbRed
    End Sub

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