'功能:设置RTBShow中显示文本的颜色
Private Sub setRTBShowColor()
    Dim Index%            'RTBShow控件的索引
    Dim iStart%           '查找的起点位置
    Dim i%, num%
    
    For Index = 0 To Dyna
        With RTBShow(Index)
            '-------------------此段为恢复,不恢复将把所有的文本都变色
                .SelStart = 0
                .SelLength = Len(.Text)
                .SelColor = RGB(0, 0, 0)
                .SelUnderline = False
            '------------------
            If ChangeColor(Index) <> "" Then               '为空说明没有要改色的,进行下一个
                getEveryChange ChangeColor(Index)          '获得改色的内容,放在everychange中
                num = UBound(EveryChange)                    '该色的次数
                iStart = 0
                
                For i = 0 To num
                    .SelStart = .SelStart + iStart    '设置位置
                    .Find EveryChange(i), .SelStart     '(1)
                    .SelColor = &HC000&                 '该为绿色
                    .SelUnderline = True                '加下划线
                    iStart = Len(EveryChange(i))        '
                Next i
            End If
        End With
    Next Index
End Sub
在1处出现问题,设置查找位置后.如果在查找位置前有与查找内容相同的文本,返回的位置是前一个文本的索引.selstart的值变小了.
不知道我说的清楚吗?大家可以帮我看看吗?什么地方出的错?
在线等~~~

解决方案 »

  1.   

        Private Sub Command1_Click()    Dim str As String    Dim Text As String    Dim Position As Integer    Dim Lenth As Integer    str = "输入要高亮显示的字符串"    Text = UCase(InputBox(str, , "s"))    If Text <> "" Then     Position = InStr(RichTextBox1.Text, Text) - 1     Lenth = Len(Text)     RichTextBox1.SelStart = Position     RichTextBox1.SelLength = Lenth     RichTextBox1.SelColor = RGB(255, 0, 0)     Do While InStr(Position + Lenth + 1, RichTextBox1.Text, Text) <> 0         Position = InStr(Position + Lenth + 1, RichTextBox1.Text, Text) - 1         RichTextBox1.SelStart = Position         RichTextBox1.SelLength = Lenth         RichTextBox1.SelColor = RGB(255, 0, 0)     Loop    End If    End SubSub FINDSTRINRTF(RTF As RichTextBox, FINDSTR As String)
    Dim POS As Long
    POS = RTF.Find(FINDSTR, , , 12)
     If POS = -1 Then MsgBox "'" & FINDSTR & "' not found !"
        RTF.SelStart = POS
        RTF.SelLength = Len(FINDSTR)
    End Sub
    Private Sub Form_Load()     Dim x(1000) As String
    For i = 0 To 1000
    x(i) = Chr(Int(Rnd * 26 + 65))
    Next     RichTextBox1.Text = Join(x, "")
    End Sub