richtextbox文本框中文字按条件变色的问题

来源:百度知道 编辑:UC知道 时间:2024/07/02 13:53:54
图中是两个richtextbox文本框,想要的效果是在下面的文本框中一个一个输入字符,如果与上面的对应字符一样,那么下面的字符变红,下面是代码,高手看看哪里错了,我只能做出最后一个字符变色,十分感谢

Private Sub richtextbox1_click()
Dim b As Integer
RichTextBox1.Text = "rice"
For b = 0 To 2 '控制当前插入点
Next
RichTextBox1.SelStart = 0 + b
RichTextBox1.SelLength = 1
If RichTextBox1.SelText = RichTextBox4.SelText Then
b = b + 1
End If
End Sub

Private Sub RichTextBox4_change()
Dim a As Integer
For a = 0 To 3 '控制当前插入点

RichTextBox4.SelStart = 0 + a
RichTextBox4.SelLength = 1
If RichTextBox4.SelText = RichTextBox1.SelText Then
RichTextBox4.SelColor = RGB(255, 0, 0)
End If
Next
a = a + 1
End Sub

如果你是要相同的位置上的字符相同,就变红,

把RichTextBox4_change的代码改为以下

Private Sub RichTextBox4_Change()
Dim L As Long
L = Len(RichTextBox4.Text)
If L = 0 Then Exit Sub
If Len(RichTextBox1.Text) < L Then Exit Sub

RichTextBox4.SelStart = L - 1
RichTextBox4.SelLength = 1

If Mid(RichTextBox1.Text, L, 1) = Right(RichTextBox4.Text, 1) Then
RichTextBox4.SelColor = vbRed
Else
RichTextBox4.SelColor = vbBlack
End If

RichTextBox4.SelStart = L
RichTextBox4.SelLength = 0
End Sub